From: Gavin Kistner Date: 2004-02-15T05:14:56+09:00 Subject: Hash like JS Hash (code) Although I may never use it, I thought I'd share the following experiment. I find mildly frustrating the need to type: myHash['foo'] to access a property of my hash, when for strings myHash.foo seems perfectly logical. (At least to me, coming from JavaScript.) So I wrote the following code, which catches any methods not available for hash and treats them like keys. (Thanks to memmove on #ruby-lang for the idea of allowing setters in addition to getters.) class Hash def method_missing(meth,*args) if /=$/=~(meth=meth.id2name) then self[meth[0...-1]] = (args.length<2 ? args[0] : args) else self[meth] end end end x = { 'name'=>'Gavin', 'age'=>31 } x.weight = 171 x.feet = ['left','right'] puts x.name , x.weight , x.foo , x.inspect PRODUCES Gavin 171 nil {"name"=>"Gavin", "weight"=>171, "feet"=>["left", "right"], "age"=>31}