Forum: Ruby Method constant arguments

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alexandre R. (Guest)
on 2007-04-07 09:47
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?

Thanks
Gavin K. (Guest)
on 2007-04-07 10:55
(Received via mailing list)
On Apr 6, 11:47 pm, Alexandre R. <removed_email_address@domain.invalid> wrote:
> 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.
Robert K. (Guest)
on 2007-04-07 11:06
(Received via mailing list)
On 07.04.2007 07:47, Alexandre R. wrote:
>
> 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?

There are tons of ways.  You could use bit mapped flags (see File or
Regexp for example).  Or you use a Hash like this

do_some_work('/', :overwrite => true, :use_id_as_filename => true)

Kind regards

  robert
Alex Y. (Guest)
on 2007-04-07 11:07
(Received via mailing list)
Alexandre R. wrote:
>
> 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.
Timothy H. (Guest)
on 2007-04-07 16:49
(Received via mailing list)
Alexandre R. wrote:
>
>
Simple solution - just use constants

Overwrite = true
UseIdAsFilename = false
Robert K. (Guest)
on 2007-04-07 17:00
(Received via mailing list)
On 07.04.2007 14:48, Timothy H. wrote:
>>   dosomework('/', true, false)
>>
>>
> Simple solution - just use constants
>
> Overwrite = true
> UseIdAsFilename = false

IMHO that's not a good solution as you still have to put them in the
proper position of the argument list.  If you use constants than they
make more sense when using bit operations (like File).

Kind regards

  robert
Alexandre R. (Guest)
on 2007-04-07 23:32
Thank you all for the answers. I really liked the solution below. It can
handle a lot of the things I want to do. I can set overwrite to true as
default for instance.
I remember keyword arguments when I began learning Python and I liked
it. Then I found Ruby and I couldnt go back ;) It would be a nice thing
to have it in Ruby.

Thanks a lot.

Gavin K. wrote:
> On Apr 6, 11:47 pm, Alexandre R. <removed_email_address@domain.invalid> wrote:
>> 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.
Rick D. (Guest)
on 2007-04-08 00:31
(Received via mailing list)
On 4/7/07, Alexandre R. <removed_email_address@domain.invalid> wrote:
> Thank you all for the answers. I really liked the solution below. It can
> handle a lot of the things I want to do. I can set overwrite to true as
> default for instance.

> Gavin K. wrote:

> > Here's one way:
> >
> >   def dosomehomework( directory, options={} )
> >     if options[:overwrite] then
> >       ...
> >   end

Keep in mind that if you want to set defaults, you probably want to do
it something like this:

def dosomehomework(directory, options={})

   options = {
       :overwrite => true,
       :otheroption => :default_value
   }.merge(options)
...
}

Instead of:

def dosomehomework(directory, options={ :overwrite => true,
:otheroption => :default_value })
...

In the second case, if the user specified any option it would wipe out
all of the defaults.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
This topic is locked and can not be replied to.