From: Joel VanderWerf Date: 2008-10-23T08:29:47+09:00 Subject: Re: neatest way to extend Struct Martin DeMello wrote: > I want a Structlike class that takes a hash constructor, so I can say > > class A < MyStruct.new(*keys); end > > a = A.new(:key1 => val1, :key2 => val2) > > Is there any way to base this off Struct? > > martin Something based on this, maybe? I hope Struct#members preserves order... class A < Struct.new(:a, :b) def initialize(h) super *h.values_at(*self.class.members.map {|s| s.intern}) end end a = A.new(:a => 1, :b => 2) p a # ==> # -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407