Strings
Double vs single quotes
Strings can sit in double or single quotes, and for plain text both behave the same. Double quotes additionally process escape sequences such as newline, while single quotes keep almost everything literally.
a = "a\nb" b = 'a\nb' puts a # prints a, then b on a new line puts b # prints a\nb literally
ruby quotes.rb
a
b
a\nb
Interpolation #{}
Inside double quotes you can embed any expression with a hash mark and curly braces. Ruby runs the code inside the braces and pastes the result into the string.
name = "Ada" puts "Hello, #{name}" # prints Hello, Ada puts "2 + 2 = #{2 + 2}" # prints 2 + 2 = 4
ruby interpolate.rb
Hello, Ada
2 + 2 = 4
Interpolation works only in double quotes. In single quotes the same braces stay untouched, which is a classic beginner surprise.
Heredocs
Long multi-line text is easier to read as a heredoc than as many quoted lines. Open with two angle signs, a tilde for the dedented form and a label, write any lines you want, then close with the same label on its own line.
text = <<~TEXT Hello, Ruby. TEXT puts text
ruby heredoc.rb
Hello,
Ruby.
Handy methods
Strings come with many small helpers. length counts characters, upcase builds a loud copy, strip trims spaces on both sides, and split cuts a string into an array of pieces.
p "Ada".length # prints 3 p "ada".upcase # prints "ADA" p " Ada ".strip # prints "Ada" p "a,b".split(",") # prints ["a", "b"]
ruby methods.rb
3
"ADA"
"Ada"
["a", "b"]
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which quotes turn
\ninto a real newline?Answer
Double quotes
Double quotes read escape sequences; single quotes keep the backslash and the n as two characters.
-
What does this program print?
name = "Ada" puts "Hello, #{name}"Answer
Hello, Ada
The interpolation runs the code in the braces and pastes its value into the string.
-
What does this program print?
name = "Ada" puts 'Hello, #{name}'Answer
Hello, #{name}
Single quotes do not interpolate, so the braces stay in the text.
-
What does
"Ada".lengthreturn?Answer
3
length counts the characters of the string, and Ada has three.
-
What does
" Ada ".stripreturn?Answer
"Ada"
strip removes the spaces at both ends and leaves the letters untouched.
-
What does
"a,b".split(",")return?Answer
["a", "b"]
split cuts the string at each comma and returns an array of the pieces.
-
What is a heredoc for?
Answer
Multi-line text that is easier to read than many quoted lines
The text sits between the opening label and the closing label, one line per line.
-
What does this program print?
puts "2 + 2 = #{2 + 2}"Answer
2 + 2 = 4
Ruby evaluates the expression in the braces and pastes 4 into the text.
-
What does
"ada".upcasereturn?Answer
"ADA"
upcase returns a new string with every letter in capitals and leaves the receiver alone.
-
In the single-quoted string
'a\nb', what are the two characters in the middle?Answer
A backslash and the letter n
Single quotes keep the backslash literally, so the line stays one line.
Article author: Arthur Isaev