Gavin Kistner
4/7/2007 6:50:00 AM
On Apr 6, 11:47 pm, Alexandre Rosenfeld <airm...@gmail.com> wrote:
> def dosomework(directory, overwrite = false, use_id_as_filename =
> true)
>
> dosomework('/', true, false)
>
> I would like to call that function with some readable parameters,
> something like this:
>
> dosomework('/', Overwrite, UseIdAsFilename)
>
> I have tried array arguments and hashes, but I didn't like those
> solutions. What would be the recommended way to do that in Ruby?
Here's one way:
def dosomehomework( directory, options={} )
if options[:overwrite] then
...
end
dosomehomework '/', :use_id_as_filename=>true, :overwrite=>false
You lose the self-documentating nature of the method arguments, but
gain clarity in the method call as well as order-independance.
Rumor has it Ruby 2.0 will have keyword arguments; I'm not sure what
the current proposal is, but iirc you'll have the best of both worlds
there.