Why must we use "#{var}" in all output results instead of var on its own?


Why must we use "#{var}" in all output results instead of var on its own?

say I want a method that prints the sound of a dog; I can do it one of many ways:


def animal_sound(snd)
puts "the dog" + snd+"s"



def animal_sound(snd)
puts "the dog #{snd}s"


Why does CA insist on using the #{} format?


String concatenation:

str1 = "Hello"
str2 = "World"

my_string = str1 + " " + str2 + "!"

puts my_string    # Hello World!

String interpolation:

my_string = "#{str1} #{str2}!"

puts my_string    # Hello World!

The hash-block syntax tells Ruby that inside the block is real code. It can be any expression or value, which will then be converted to string type.


Thank you for that breakdown mtf; kind of reminds me of PHP pages where the
php code can be dropped pretty much anywhere withing an html code as long
as the proper php wrappers are used.

But this still does not answer my question on why CA insists on using only
that output format, when they themselves claim there are many ways to solve
a problem. I mean as long as person understands the core concept, it should
not matter how they output that same result. Thoughts?


For best results, follow instructions to the letter with no variation or embellishment. This course cannot be edited so it is what it is. The author may not have considered other options when writing the SCT for the exercise. This is quite typical. I find doing what they insist is best.

And for this site, style guide and best practices are not really up for discussion. It's too soon to start abstract discussions when there is still so much of the fundamentals to cover.


Fair point, yet many people taking this course come half or fully seasoned
from other languages. And in being so there some relatively global
standards when it comes to working with common data types. My concern is
not as much with following a certain 'style' of coding at the beginning but
rather the fact an answer can be marked wrong when it's not actually wrong.
I would say a clearer type of notation should be made regarding these minor
elements. Like if a note was made stating that "although concatenation
would also work here and there...please stick to interpolation" people
would not be loosing their marbles over correct answers yet variable output
methods for that answer. But yea yeah free course take or leave.. just
wanted to get this off my chest. =)


I've had math and physics teachers who were sticklers for us following their method. How else can they tell if one has got it? If the question asks for x using y, don't give x using z, sort of thing.


Your two methods have different output though (last two lines are the output)

echo '
def animal_sound(snd)
    puts "the dog" + snd+"s"


def animal_sound(snd)
    puts "the dog #{snd}s"

' | ruby
the dogbarks
the dog barks


@ionatan, typo on my part, but you get the drift.


I doubt that anybody insists in doing it this way. You are free to use
what you prefer.

BTW, your two versions are not equivalent. Try to call


In the ex1 version, you get an exception, in the ex2 version you don't
get an exception. Now, depending on what behaviour you prefer, either
the first or the second one is better for you IN THIS PARTICULAR CASE.

BTW, I use both forms in my program, but I use the string interpolation

{....} much more frequently.



This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.