Tuesday, September 20, 2011

Interesting Ruby Tidbits

In Ruby,

@@var class variables are shared between the class, its instances, and any extended classes; thus they act like globals within a superclass' hierarchy.

do...end blocks bind at a lower precedence than { .. } blocks; thus,
p a.map {|s|   s*2 }
p a.map do |s| s*2 end

"string"  is equivalent to %(string) and %Q(string), but the latter two allow nesting of quote characters without special escaping

To get a list of user methods from a subclass, use the cls.methods method and subtract the base class method list from the class method list.

If you use the default argument to Hash.new("default"), the value gets shared among all defaulted hash entries, so assignment to any of them will change all of them. Use the block form of default initialization instead: { |h,k| h[k] = "default" }

Post a Comment