On Fri, 24 Mar 2006, Eli Bendersky wrote:

> Hello all,
>
> I'm now writing my first real Ruby module, and on the second function I
> already run into a pattern that seems to be very common.
> I like using keyword arguments for methods, as follows:
>
> def method(args)
>  ...
> end
>
> And then call:
>
> method(:arg1 => value1, :arg2 => value2)
>
> And so on.
> Now, often some arguments are compulsory, so I wrote the following code
> to verify it:
>
> def method(args)
>  [:username, :password, :url].each do |arg|
>    raise(ArgumentError, "Argument :#{arg} is compulsory" unless
> args.has_key?(arg)
>  end
>
>  ...
> end
>
> I have two questions:
> 1) Is this the right/idiomatic/best way to achieve what I'm attempting
> ?
> 2) Is there a library that encapsulates this capability, or is everyone
> writing one of his own ? Because if I don't find any, I surely will
> write one... It should be enough saying:
>
> verify_args(:username, :password, :url)
>
> Instead of the .each do iteration everywhere
>
> Eli

http://codeforpeople.com/lib/ruby/
http://codeforpeople.com/lib/ruby/parseargs/parseargs-0.3.0/README

regards.

-a
-- 
share your knowledge.  it's a way to achieve immortality.
- h.h. the 14th dali lama