From: Robert Klemme Date: 2007-11-16T00:52:58+09:00 Subject: Re: Struct constructor 2007/11/15, Wayne Magor : > It isn't possible to define a class method [] (to make a constructor) > for a struct is it? > > If you have: > > Customer = Struct.new( "Customer", :name, :address, :zip ) > joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 ) > > but you want it to be: > > joe = ["Joe Smith", "123 Maple, Anytown NC", 12345] > > Ruby will just make joe an array, right? Of course you cannot change the semantics of the array constructor. > No way around this that I > can see, is there? Of course there is, this is Ruby. :-) irb(main):001:0> Test = Struct.new :foo, :bar do irb(main):002:1* class < alias :initialize :[] irb(main):004:2> end irb(main):005:1> end => Test irb(main):006:0> Test[1,2] => # irb(main):007:0> Kind regards robert -- use.inject do |as, often| as.you_can - without end