On 2/9/06, David Vallner <david / vallner.net> wrote:

> have grown
> quite accustomed to using the more flexible "options hash" pattern instead.
> For example, if you have some class with instance variables bar, baz, and
> quux:
>
>         class Foo
>                 DEFAULTS = {
>                         :bar => 1,
>                         :baz => 2,
>                         :quux => 3
>                 }
>
>                 attr :bar
>                 attr :baz
>                 attr :quux
>
>                 def initialize(params)
>                         attribs = DEFAULTS.dup.update(params)
>                         @bar = attribs[:bar]
>                         @baz = attribs[:baz]
>                         @quux = attribs[:quux]
>                 end
>         end
>
>         foo = Foo.new(:bar = "Hello", :quux => "World")
>
>         p foo # Outputs #<Foo:0xb7c8004c @bar="Hello", @quux="World", @baz=2>
>
> I find this covers 90% of what you commonly use overloaded constructors for,
> and is a bit more readable too.

I'd never seen this before. Cool!

I think you can use
attribs = DEFAULTS.merge(param)
instead of
attribs = DEFAULTS.dup.update(params)

--
R. Mark Volkmann
Partner, Object Computing, Inc.