Main Content

String interpretation in Ruby

Archive - Originally posted on "The Horse's Mouth" - 2008-03-21 18:23:43 - Graham Ellis

Ruby's rather clever in terms or how it handles string interpretation. Since variable names only start with special characters sometimes, it can't use Perl's trick of making the $ and @ character special in a string. So instead it uses a #{.....} notation. For example:

puts "Tocker is #{tocker}"
uses a local variable.

puts "Ticker is #{$ticker}"
uses a global variable

and

puts "Sign #{noentrysign.colour} and area #{noentrysign.area}"
calls an instance or accessory method on the object. There is a complete example of that code here.