From: Jacob Fugal Date: 2006-05-13T00:48:02+09:00 Subject: Re: Unit testing with mock objects On 5/12/06, Jacob Fugal wrote: > I like the "trailing option hash" "pattern" (used loosely). It's > pretty close to what you've got, but syntactlcally a little nicer. I'd > do it like this for your demonstrated class: > > class Foo > DEFAULTS = { > :domain_obj1 => DomainObj1, > :domain_obj2 => DomainObj2 > } > > def initialize(x, y, options={}) > options.merge!(DEFAULTS) > @domain_obj1 = options[:domain_obj1] > @domain_obj2 = options[:domain_obj2] > @domain_obj1 = @domain_obj1.new if Class === @domain_obj1 > @domain_obj2 = @domain_obj2.new if Class === @domain_obj2 > end > end Shame on me for posting without testing... I got my merge backwards, it should be: options = DEFAULTS.merge(options) Since the values in the argument, not receiver, take precedence if both have a key. Jacob Fugal