# Hash-style parameters

**URL:** <https://www.ruby-forum.com/t/hash-style-parameters/193284>\
**Category:** Ruby\
**Created:** [August 14, 2010, 8:23pm UTC](https://www.ruby-forum.com/t/hash-style-parameters/193284 "2010-08-14T20:23:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 14, 2010, 8:23pm UTC](https://www.ruby-forum.com/t/hash-style-parameters/193284/1 "2010-08-14T20:23:18Z")

</div>

Some function calls I’ve seen, like in Shoes, look like so:

para “Paragraph Text”, :style =\> “this”, :whatever =\> “that”

How do I define a function call so that can take one “normal” parameter  
and then an unknown number of key/value pairs?

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 14, 2010, 8:58pm UTC](https://www.ruby-forum.com/t/hash-style-parameters/193284/2 "2010-08-14T20:58:29Z")

</div>

On Saturday, August 14, 2010 01:23:21 pm Terry M. wrote:

> Some function calls I’ve seen, like in Shoes, look like so:
> 
> para “Paragraph Text”, :style =\> “this”, :whatever =\> “that”
> 
> How do I define a function call so that can take one “normal” parameter  
> and then an unknown number of key/value pairs?

You could answer your own question by looking at the source of the  
program  
doing this – and I’d suggest it for anything you want to learn more  
about.

For this case, remember that this basically becomes:

para(“Paragraph Text”, {:style =\> “this”, :whatever =\> “that”})

And of course, there’s no reason those literals need to be there – you  
could  
just do this:

text = “Paragraph Text”  
hash = {:style =\> “this”, :whatever =\> “that”}  
para(text, hash)

So, it should be clear that this is what you want:

def para text, options

There is one catch, though – you said “unknown number”, which could be  
zero,  
so you need a default value:

def para text, options={}

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 15, 2010, 12:50am UTC](https://www.ruby-forum.com/t/hash-style-parameters/193284/3 "2010-08-15T00:50:51Z")

</div>

On Sat, Aug 14, 2010 at 13:58, David M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So, it should be clear that this is what you want:
> 
> def para text, options
> 
> There is one catch, though – you said “unknown number”, which could be zero,  
> so you need a default value:
> 
> def para text, options={}

Just a note: while you can omit parens in method definitions, most  
code does only if the method takes no arguments, if it does, leave the  
parens there.

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 15, 2010, 7:56am UTC](https://www.ruby-forum.com/t/hash-style-parameters/193284/4 "2010-08-15T07:56:52Z")

</div>

On Saturday, August 14, 2010 05:50:22 pm Pablo Torres N. wrote:

> Just a note: while you can omit parens in method definitions, most  
> code does only if the method takes no arguments, if it does, leave the  
> parens there.

Why?

I find it more readable without parens, but with syntax highlighting,  
mostly  
because that’s how I call methods.

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 16, 2010, 11:18pm UTC](https://www.ruby-forum.com/t/hash-style-parameters/193284/5 "2010-08-16T23:18:00Z")

</div>

> def para text, options
> 
> There is one catch, though – you said “unknown number”, which could be  
> zero,  
> so you need a default value:
> 
> def para text, options={}

There is also the “named parameter” gem which might be useful to you:

> **[GitHub - rdp/arguments: You don't have to wait until Ruby 2.0 to get...](https://github.com/rdp/arguments)**
>
> You don't have to wait until Ruby 2.0 to get (named|keyword) arguments. Has been tested with 1.8.6 and 1.9.1 - GitHub - rdp/arguments: You don't have to wait until Ruby 2.0 to get (named|ke...

---

<div class="post-metadata">

**Author:** ![callyworld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@callyworld](https://www.ruby-forum.com/u/callyworld)\
**Post date:** [August 15, 2010, 6:10pm UTC](https://www.ruby-forum.com/t/hash-style-parameters/193284/6 "2010-08-15T18:10:50Z")

</div>

On Sun, Aug 15, 2010 at 00:56, David M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Just a convention, I guess precisely so you can easily tell method  
definitions and method invocations apart. Consider:

def say\_hi(tittle, name)  
puts “Hi, #{tittle} #{name}”  
end

tittle = “Dr.”  
name = “Cham”

some\_obj.instance\_eval do  
def say\_hi tittle, name  
puts “Howdy, #{tittle} #{name}”  
end  
end

A bit ambiguous.
