October 2006
Intermediate to advanced
888 pages
16h 55m
English
The #{} notation makes this easy. We need not worry about converting, appending, and concatenating; we can interpolate a variable value or other expression at any point in a string:
puts "#{temp_f} Fahrenheit is #{temp_c} Celsius"
puts "The discriminant has the value #{b*b - 4*a*c}."
puts "#{word} is #{word.reverse} spelled backward."Bear in mind that full statements can also be used inside the braces. The last evaluated expression will be the one returned.
str = "The answer is #{ def factorial(n)
n==0 ? 1 : n*factorial(n-1)
end
answer = factorial(3) * 7}, of course."
# The answer is 42, of course.There are some shortcuts for global, class, and instance variables, in which case the braces can be dispensed ...
Read now
Unlock full access