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


#1

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:

ex1:

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

animal_sound("bark")

ex2:

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

animal_sound("bark")

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


#2

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.


#3

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?


#4

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.


#5

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. =)


#6

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.


#7

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

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

animal_sound("bark")

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

animal_sound("bark")
' | ruby
the dogbarks
the dog barks

#8

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


#9

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

animal_sound(4711)

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.

Ronald


#10

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