From: Alex Young Date: 2007-04-07T16:06:21+09:00 Subject: Re: Method constant arguments Alexandre Rosenfeld wrote: > Hi, I'm new to ruby, and I'm loving it so far. > > I want to call a method with some parameters that would indicate a > state, so most of them would be only a true or false value, however > that's not very good to read when calling the method. Like this: > > 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? I've seen code that does this: def dosomework(directory, *opts) if opts.include? :overwrite dosomething end if opts.include? :use_id_as_filename dosomethingelse end dofoo(directory) end I don't know if that's useful at all... it'd certainly be quite easy to write a Struct-like class that you could use in place of the opts array if you wanted to tidy it up. -- Alex