From: Mauricio Fernandez Date: 2006-06-01T22:56:53+09:00 Subject: Re: An alternative to the class Foo < Struct.new(vars) idiom and SuperStruct On Thu, Jun 01, 2006 at 09:33:29PM +0900, Jeff Rose wrote: > I'm sold. This will be great for easily defining event types in > EventMachine. Why the complicated default argument values though? For > simple cases how about just using hash syntax: > > MyClass = SuperClass.new(:a => 2, :b => 3) The problem with this is that it makes positional arguments impossible, since the Hash doesn't preserve the order; maybe MyClass = SuperClass.new(:a, 2, :b, 3) or MyClass = SuperClass.new(:a, :b, :a => 2, :b => 3) # (don't like the repetition though) ? Anyway, I realized I can remove a couple lines from SuperClass to turn the following (which I forgot to show in the previous msg) into the preferred style: # b is optional only when using the keyword arg style class Foo < SuperClass.new(:a, :b) def initialize(*args) super @b ||= "foo" end end which will look better than passing a block to SuperClass.new for most people. Maybe I should allow both styles: * mandatory keyword arguments, with default values specified in the SuperClass.new call (have to define how, two possibilities given above but there's probably a better way) * either full positional arguments or keyword arguments (at least one required to discriminate), default values set manually in #initialize as in the above example. The desired one could be specified either via an explicit call (SuperClass.new_kw_args vs. SuperClass.new_positional ?) or implicitly by the arguments given to SuperClass.new. It's all easy to implement but there are many options... -- Mauricio Fernandez - http://eigenclass.org - singular Ruby