Ruby strings could be understood as a group of characters. A string could be of length 1 (one character), but its usually longer. A string is always defined in double qoutes.
This means a string variable in ruby could hold text: words, sentences, books
Programming languages have variables. These variables have a data type
A variable could be of the data type string.
In the example below we use ruby to print text.
First define a string variable, then print the variable.
str = String.new("This is a string example")
Save as strings-example.rb. Then run with
This should output:
Generally there are 2 ways to print multiple lines.
Method 1. call display function x times,
Method 2. use the newline character inside the string
Both of these are common in programming.
# method 1: multiple calls
# method 2: newline characters
puts "Hello World"
- Create a program with multiple string variables
- Create a program that holds your name in a string.