Interpolating the value of a variable

The following statement is made in the pick ax book:

We’ll use the # character in the string to interpolate the value of
these instance variables.

class Song
def to_s
“Song: #@name–#@artist (#@duration)”
end
end

If I had been doing what they appear to be doing, I would have enclosed
the respective variable names in braces. I’m sure that they are trying
to tell me something by omitting the braces; but, I’m not sure what. In
fact, without the braces, I don’t see how it even works. Can someone
please clarify for me how they are able to omit the braces and the
significance of doing so? Thanks for any input.

  ... doug

It does in fact work for instance variables, the syntax is just not as
common.

Jonan S. wrote in post #1123310:

It does in fact work for instance variables, the syntax is just not as
common.

Thanks for the input. That’s interesting nickel knowledge. At least
now I won’t be saying, “What!!!” the next time that I see it. I may
actually use it in some of my code so that I can impress whoever might
be reading it (I have no idea who that would be.) :slight_smile: Anyway, thanks
again for the clarification.

  ...doug

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs