named method arguments with defaults

E

eliben

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
 
J

Jules Jacobs

You can use merge:

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

# do stuff
end

a_method:)size => 44)

*should* work.

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


Jules
 
E

eliben

Jules said:
You can use merge:

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

# do stuff
end

a_method:)size => 44)

*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
 
J

Jules Jacobs

Language support for named arguments will be in Ruby 2.0 (hopefully :p)

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


Jules
 
G

Gene Tani

Jules said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top