From: "David A. Black" Date: 2008-10-23T08:34:07+09:00 Subject: Re: neatest way to extend Struct Hi -- On Thu, 23 Oct 2008, 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? Do you need to use inheritance, as opposed to just instantiating MyStruct directly? I'm thinking of, for example: class MyStruct < Struct def self.new(*keys) s = super s.class_eval do define_method(:initialize) do |hash| hash.each {|k,v| send("#{k}=",v) } end end s end end A = MyStruct.new(:a,:b) a = A.new(:a => 1, :b => 2) p a.b # 2 David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL Advancing with Rails January 19-22 Fort Lauderdale, FL * * Co-taught with Patrick Ewing! See http://www.rubypal.com for details and updates!