From: Robert Klemme Date: 2007-09-04T19:58:03+09:00 Subject: Re: Struct is cleverer than the documentation claims 2007/9/4, Paul Butcher : > The Pickaxe book asserts that it will be possible to pass a block to > Struct.new in Ruby 1.9. While spelunking the code, I discovered that the > functionality has actually been present since 1.8.3. I've blogged about > this here: > > http://www.texperts.com/2007/09/03/hidden-feature-in-rubys-struct/ > > But reading this mailing list, I notice that there's another way to > achieve a similar effect, specifically deriving from Struct.new. > > So, both of the following achieve broadly the same effect: > > > Customer = Struct.new(:name, :address) do > > def to_s > > "Customer called '#{name}' living at '#{address}'" > > end > > end > > > class Customer < Struct.new(:name, :address) > > def to_s > > "Customer called '#{name}' living at '#{address}'" > > end > > end > > Which leads to the question, what *are* the differences, and should I > prefer one approach over the other? Well, difference is that you get two classes vs. one class. I frequently use the first idiom because I do not see the point in creating two classes for this scenario. IMHO a base class makes sense only if you want to inherit multiple times or separate concerns. Neither is the case here. Kind regards robert