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.
Ffe43527f5bf887e2bfecb01edc5a21c?d=identicon&s=25 Alexandre Rosenfeld (airmind)
on 2007-04-07 07: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
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-04-07 08:55
(Received via mailing list)
On Apr 6, 11:47 pm, Alexandre Rosenfeld <airm...@gmail.com> 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.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-04-07 09:06
(Received via mailing list)
On 07.04.2007 07:47, Alexandre Rosenfeld 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
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-04-07 09:07
(Received via mailing list)
Alexandre Rosenfeld 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.
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Timothy Hunter (Guest)
on 2007-04-07 14:49
(Received via mailing list)
Alexandre Rosenfeld wrote:
>
>
Simple solution - just use constants

Overwrite = true
UseIdAsFilename = false
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-04-07 15:00
(Received via mailing list)
On 07.04.2007 14:48, Timothy Hunter 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
Ffe43527f5bf887e2bfecb01edc5a21c?d=identicon&s=25 Alexandre Rosenfeld (airmind)
on 2007-04-07 21: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 Kistner wrote:
> On Apr 6, 11:47 pm, Alexandre Rosenfeld <airm...@gmail.com> 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.
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-04-07 22:31
(Received via mailing list)
On 4/7/07, Alexandre Rosenfeld <airmind@gmail.com> 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 Kistner 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.