# Keyword parameters

**URL:** <https://www.ruby-forum.com/t/keyword-parameters/140206>\
**Category:** Ruby\
**Created:** [June 11, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/keyword-parameters/140206 "2008-06-11T21:22:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [June 11, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/keyword-parameters/140206/1 "2008-06-11T21:22:45Z")

</div>

Hi everyone,

I have a question about keyword parameters in Ruby. I know that v1.8.6  
doesn’t do this and v1.9 does, and that you can ‘trick’ v1.8.6 into  
doing it with hashes.

My question is whether you can do this code:

def aProc(options = {})  
a = options[:a] || 5  
b = options[:b] || 6  
c = options[:c] || 7  
print a, b, c  
end

in such a way where you wouldn’t have to explicitly say that the local  
variable a is equal to the value in the hash with symbol :a? In other  
words, to do something like iterate through the hash and turn all of the  
symbol keys into local variable with their respective values?

Thanks,

Glenn

----- Original Message ----  
From: Jason R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Monday, March 3, 2008 4:36:49 PM  
Subject: Re: Why is this not implemented in Ruby

Show me a language that does allow you to do this, I’ve never seen it.  
Even then, Ruby doesn’t deal with method overloads via parameter  
lists, so there’s a fundamental reason why it won’t work:

def foo(a)  
end

def foo(a, b)  
end

def foo(a, b, c)  
end

foo(1) # =\> ArgumentError: wrong number of arguments (1 for 3)

Ruby 1.9 has keyword arguments, so you’ll be able to get past that  
easily then. You can fake it in 1.8 with a hash:

def aProc(options = {})  
a = options[:a] || 5  
b = options[:b] || 6  
c = options[:c] || 7  
print a, b, c  
end

aProc(:a =\> 1, :c =\> 3)

With Ruby 1.9 that will look like (I think, doing this from memory):

aProc(a: 1, c: 3)

Jason

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [June 12, 2008, 12:28am UTC](https://www.ruby-forum.com/t/keyword-parameters/140206/2 "2008-06-12T00:28:48Z")

</div>

On Jun 11, 2008, at 21:21, Glenn wrote:

> [T]o do something like iterate through the hash and turn all of the  
> symbol keys into local variable with their respective values?

No.  
What you _can_ do, however, is to simply refer to each of these  
options directly in the hash, making the hash values default instead.  
The naïve way:

options[:a] ||= 5  
…

However, a more preferable way, however, is defining a hash with the  
default, and then letting the options hash override these:

options = {:a =\> 5, …}.merge(options)

You can then access each option as option[:a] … There are a few  
caveat with this, but these aren’t usually a problem.

---

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [June 12, 2008, 1:04am UTC](https://www.ruby-forum.com/t/keyword-parameters/140206/3 "2008-06-12T01:04:43Z")

</div>

What makes an _actual_ keyword argument, and why don’t symbols suffice?

---------------------------------------------------------------|  
~Ari  
“I don’t suffer from insanity. I enjoy every minute of it” --1337est  
man alive
