From: Joel VanderWerf Date: 2006-03-26T06:08:12+09:00 Subject: Re: Verifying keyword arguments Ross Bamford wrote: ... > Not sure about libraries (though I'm sure they are out there, check RAA > and Rubyforge) but here's a little trick I've used before: > > args = { :one => 'two', :three => 'four' } > # => {:three=>"four", :one=>"two"} > > h = Hash.new { |h,k| raise ArgumentError, "#{k}" }.merge(args) > # => {:three=>"four", :one=>"two"} > > h[:one] > # => "two" > > h[:three] > # => "four" > > h[:two] > ArgumentError: two > from (irb):5 > from (irb):11 > > This way, you never actually check the arguments explicitly, but you'll > be told if your method uses one that wasn't passed. Obviously for > optional arguments you go to the original hash. > What about using rescue for optional arguments? It might be slower though. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407