Friday, July 27, 2012

#{ something }

number = 5
"The number is #{number}."
"The number is #{5}."
"The number after #{number} is #{number.next}."
"The number prior to #{number} is #{number-1}."
"We're ##{number}!"

#{ number } will get whatever you assign to the variable number.

"...Any text kept within the brackets of the special marker #{} (that is, #{text in here}) is interpreted as a Ruby expression. The result of that expression is substituted into the string that gets created. If the result of the expression is not a string, Ruby calls its to_s method and uses that instead."

(Source -- Ruby Cookbook)

No comments:

Post a Comment