From: Joel VanderWerf Date: 2006-11-30T16:29:06+09:00 Subject: Re: Wrong results using named arguments Joel VanderWerf wrote: > ara.t.howard@noaa.gov wrote: >> On Thu, 30 Nov 2006, Jason Vogel wrote: >> >>> Okay, I have a couple of questions. If I use the Hash approach, >>> >>> - How do I handle argument default values? >> >> def the_method opts = { :arg => 'default value', :another_arg => >> 'default value too' } > > Not really, at least in 1.8. That sets the default for _opts_, not for > each of the named args. This is one way to handle defaults for keyword args: DEFAULTS = { :x => 1, :y => 2 } def m opts = {} opts = DEFAULTS.merge(opts) p opts end m :x => 5 # ==> {:x=>5, :y=>2} Note that #merge is non-destructive (unlike #update, aka #merge!), so it doesn't affect DEFAULTS. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407