E
Eli Bendersky
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,
assword, :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,
assword, :url)
Instead of the .each do iteration everywhere
Eli
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
And so on.
Now, often some arguments are compulsory, so I wrote the following code
to verify it:
def method(args)
[:username,
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
Instead of the .each do iteration everywhere
Eli