From: Joel VanderWerf Date: 2006-11-30T16:16:46+09:00 Subject: Re: Wrong results using named arguments 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. def the_method opts = { :arg => 'default value', :another_arg => 'default value too' } p opts end the_method :arg => "qwe" # ==> {:arg=>"qwe"} Note that the hash doesn't retain the default value for :another_arg. Has this changed in 1.9? Is there a way to set the defaults for keyword args? ... >> - Is there a way to enumerate the valid choices for values (e.g. colors >> = [red,white,blue]) > > def the_method opts = { :a => %w( a b c ).first, :b => %w( d e f > ).first } You know this gets evaluated every time you call the_method, right? def m opts = { :foo => (puts "called m") }; end m; m output: called m called m Anyway, this serves no purpose beyond documentation (it doesn't validate). Why not just put this info into the rdoc for the method? -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407