In learning ruby the hard way, i am on exercise 6, and in some of the lines he puts
puts x
puts y
puts “I said: #{x}.”
puts “I also said: ‘#{y}’.”
and when i input that to atom and open the file in powershell, it will say puts “i said: #{x}.”
but i am trying to understand why i would put puts x or puts y before the actual line i am printing.
The string interpolation characters #{}
will print what ever value into the string so
x = 4
puts "I said #{x}"
becomes I said 4
. String interpolation simply allows some Ruby’s output to be placed in a string. For example:
puts "2 + 2 is #{2 + 2}"
Will give us 2 + 2 is 4
A normal puts x
would just put the value 4
out from what x
had stored in it and that is all. We don’t see any explanation text, just a value printed.