From: Tim Hunter Date: 2004-02-15T11:59:55+09:00 Subject: Re: Hash like JS Hash (code) On Sun, 15 Feb 2004 02:47:07 +0000, Tim Sutherland wrote: > In article <8hvXb.37945$uV3.63269@attbi_s51>, Gavin Kistner wrote: >>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.) > [...] >>x = { 'name'=>'Gavin', 'age'=>31 } >>x.weight = 171 >>x.feet = ['left','right'] >>puts x.name , x.weight , x.foo , x.inspect > [...] > > Are you familiar with the `Struct` class? > > Person = Struct.new(:name, :weight, :age, :feet) > > gavin = Person.new('Gavin', 171, 31, ['left', 'right']) gavin.name # -> > 'Gavin' > gavin.weight # -> 171 > gavin.age += 31 # -> 32 Also the OpenStruct class. Check out ostruct.rb.