# Named method arguments with defaults

**URL:** <https://www.ruby-forum.com/t/named-method-arguments-with-defaults/79824>\
**Category:** Ruby\
**Created:** [March 11, 2006, 8:49am UTC](https://www.ruby-forum.com/t/named-method-arguments-with-defaults/79824 "2006-03-11T08:49:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 11, 2006, 8:49am UTC](https://www.ruby-forum.com/t/named-method-arguments-with-defaults/79824/1 "2006-03-11T08:49:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 11, 2006, 10:23am UTC](https://www.ruby-forum.com/t/named-method-arguments-with-defaults/79824/2 "2006-03-11T10:23:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 11, 2006, 10:50am UTC](https://www.ruby-forum.com/t/named-method-arguments-with-defaults/79824/3 "2006-03-11T10:50:41Z")

</div>

Jules J. 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 11, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/named-method-arguments-with-defaults/79824/4 "2006-03-11T16:39:00Z")

</div>

Jules J. 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 S. which i  
can’t seem to google for.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 11, 2006, 11:39am UTC](https://www.ruby-forum.com/t/named-method-arguments-with-defaults/79824/5 "2006-03-11T11:39:18Z")

</div>

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
