Tue, 28 Jul 09

Harmonious Hashing

Working in ActionScript and JavaScript, one gets used to getting and setting known Object properties using the dot-accessor.

var h = {}
h.foo = "bar";

For the lazy and the brave, the same can be accomplished in Ruby.

class Hash
  def method_missing(m, *args)
    if args.size > 0 
      self[m] = args.first
    elsif self.include?(m)
      self[m] 
    elsif self.include?(m.to_s)
      self[m.to_s]
    end  
  end
end

The newly assigned key is a Symbol, a desirable default.

Tue, 28 Jul 09