I am just starting to learn Ruby (first programming) and have a basic syntax question regarding variables and various ways of writing code.
Chris Pine "Learn to Program" taught me how to write a basic program like this ...
num_cars_again= 2 puts 'I own ' + num_cars_again.to_s + ' cars.'
This is great, but then I came across a tutorial on ruby.learncodethehardway.com and learned to write the exact same program like this ...
num_cars= 2 puts "I own #{num_cars} cars."
Both of them output the same thing, but obviously option 2 is a much shorter way to do this.
Is there any special reason why I should use one format over another?
ruby string-concatenation string-interpolation
Jeff H. Apr 09 2018-12-12T00: 00Z
source share