From: Mark Volkmann Date: 2006-02-10T06:12:36+09:00 Subject: Re: yaml object de-serialisation On 2/9/06, David Vallner 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 # > > 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.