Forum: Ruby named method arguments with defaults

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.
Fc761ccaf6c0d7d977e2959f9bfebd06?d=identicon&s=25 unknown (Guest)
on 2006-03-11 08:49
(Received via mailing list)
Hello all,

What is the current idiomatic approach to pass named arguments to a
method in Ruby ?

In Perl, I can do:

sub method
{
  # first the defaults are specified, then @_ is appended and overrides
  # the defaults, using Perl's array-folding-into-hash feature
  #
  my %args = ("size" => 45,
                      "length" => 12,
                      @_);

  my $the_size = $args{"size"};
}

# now, a call: 44 will override the default size 45, but length will
remain 12
#
method("size" => 44);

I'm sure Ruby has a nearly similar approach to achieve this, I just not
skilled enough (yet) to know how. Please advise.

Thanks in advance
Eli
9539774fe19b5268e39ee6cf3ca19b71?d=identicon&s=25 Jules Jacobs (Guest)
on 2006-03-11 10:23
(Received via mailing list)
You can use merge:

def a_method(*options)
  options = {:size => 45, :length => 12}.merge(options)

  # do stuff
end

a_method(:size => 44)

*should* work.

On Saturday, March 11, 2006, at 4:48 PM,  wrote:
>  # the defaults, using Perl's array-folding-into-hash feature
>#
>method("size" => 44);
>
>I'm sure Ruby has a nearly similar approach to achieve this, I just not
>skilled enough (yet) to know how. Please advise.
>
>Thanks in advance
>Eli
>
>


Jules
Fc761ccaf6c0d7d977e2959f9bfebd06?d=identicon&s=25 unknown (Guest)
on 2006-03-11 10:50
(Received via mailing list)
Jules Jacobs wrote:
> *should* work.
>

This works when I remove the asterisk from the (*options).
Makes sense, I guess, since when I call:

a_method(:size => 21, :weight => 22)

With the asterisk specified, the arguments are folded into an array,
and merge can't take an array as an argument when called on a Hash.
Without the asterisk, Ruby's "special features" takes the arguments I
passed to a_method as a Hash, since nothing came after the last x => y
assignment, and then merge works.

Did I decipher this correctly ?

In any case, your proposal (without the asterisk) works nicely. In the
1st edition of "Programming Ruby" the authors noted that by Ruby 1.8,
named arguments will be built into the language. I assume this hasn't
happened yet ?

Thanks
Eli
9539774fe19b5268e39ee6cf3ca19b71?d=identicon&s=25 Jules Jacobs (Guest)
on 2006-03-11 11:39
(Received via mailing list)
Language support for named arguments will be in Ruby 2.0 (hopefully :P)

On Saturday, March 11, 2006, at 6:48 PM,  wrote:
>> a_method(:size => 44)
>and merge can't take an array as an argument when called on a Hash.
>
>Thanks
>Eli
>
>


Jules
9dfe8c734b0f9b37a4e218425c0a2138?d=identicon&s=25 Gene Tani (Guest)
on 2006-03-11 16:39
(Received via mailing list)
Jules Jacobs wrote:
> Language support for named arguments will be in Ruby 2.0 (hopefully :P)
>

There's a discussion of how to simulate keyword args in the draft Ruby
Cookbook, based on a KeywordProcessor module by Gavin Sinclair which i
can't seem to google for.
This topic is locked and can not be replied to.