Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Ruby variable interpolation

To further improve the situation we are going to use the interpolation capabilities of Ruby. We can insert a hashmark (#) followed by a variable between a pair of curly braces in a string. Ruby will replace that whole construct with the value of the variable.

person = "Foo"

puts "In double quotes #{person}"
puts 'In single quotes #{person}'

Here however the behavior of double-quote and single-quote is different. The interpolation only works in double-quoted strings. Single-quoted strings will just include the hash-mark, the opening curly, the variable name and the closing curly.

In double quotes Foo
In single quotes #{person}