Method signature - do I use named parameters OR pass a hash for flexibility/readability?

Hi,

What is the best practice in Ruby regarding creating methods? Is it to
create a named set of parameters to pass (specific method signature) OR
should I be passing a hash which may be more readable/future proof? For
example:

Option 1 - Named
   - def this_method (time, search_string, something_else)
   - a = this_method (11, "asdf", "asdf asdfa")

Option 2 - Hash
   - def this_method (params)
   - a = this_method (:time => 11, :search_string => "asdf", :something_else
=> "asdf asdfa")

Option 2 seems to be more readable and flexible from what I can see. Is
this the recommended Ruby best practice for implementing Ruby methods that
have more than a couple of parameters which have to be passed?

Regards
Greg

Don't forget option 3: a few required positional parameters followed
by a hash of optional parameters. This is an approach used by Rails,
and it's a nice compromise for methods with a lot of parameters.

···

On 4/12/07, Greg Hauptmann <greg.hauptmann.ruby@gmail.com> wrote:

Option 1 - Named
   - def this_method (time, search_string, something_else)
   - a = this_method (11, "asdf", "asdf asdfa")

Option 2 - Hash
   - def this_method (params)
   - a = this_method (:time => 11, :search_string => "asdf", :something_else
=> "asdf asdfa")

--
Avdi

Option 3

     - def this_method (required_param, options = {})

   - a = this_method ('required value', :time => 11)

Option 4

   - def this_method (params = {:time => 'default value', :search_string
=> 'default value})

   - a = this_method (:time => 11)

I prefer this approach to use hash params (named hashed params). It's
easier to know the method's parameters, you don't need read the method
body, to knows the parameters names

It's easier to implement tests (TDD), you don't have N possibilities to
the parameteres, because you have default values

···

--
Posted via http://www.ruby-forum.com/.

Pablo Cantero wrote in post #962629:

Option 4

   - def this_method (params = {:time => 'default value', :search_string
=> 'default value})

Option 4a is better I think:

  def this_methods(params = {})
    params = {
                 :time=>'default value',
                 :search_string=>'default value',
             }.merge(params)

···

--
Posted via http://www.ruby-forum.com/\.