« Some Ruby programming examples from our course | Main | Dynamic Memory Allocation in C - calloc, realloc »
March 21, 2008
String interpretation in Ruby
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.
Posted by gje at March 21, 2008 06:23 PM