# Role of options={}?

**URL:** <https://www.ruby-forum.com/t/role-of-options/182852>\
**Category:** Rails\
**Created:** [February 3, 2010, 9:21pm UTC](https://www.ruby-forum.com/t/role-of-options/182852 "2010-02-03T21:21:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 3, 2010, 9:21pm UTC](https://www.ruby-forum.com/t/role-of-options/182852/1 "2010-02-03T21:21:40Z")

</div>

Hey all,

Can’t find a good explanation of options={}. Specifically I’m not sure  
of its role in this given line of code:

helper file:

def render\_show\_actions(resource, actions, options={})  
commands = actions.map do |action|  
send “render\_show\_action\_#{action}”, resource, options  
end

controller:

def render\_show\_action\_pass(student, options={})  
@template.link\_to “pass”, pass\_student\_path(student), :method =\>  
:get  
end  
helper\_method :render\_show\_action\_pass

It appears that the action in the helper dynamically builds the action  
in controller. But then the question is what is options doing there and  
which method is calling which?

Thanks for any suggestions.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 3, 2010, 9:47pm UTC](https://www.ruby-forum.com/t/role-of-options/182852/2 "2010-02-03T21:47:28Z")

</div>

John M. wrote:

> Hey all,
> 
> Can’t find a good explanation of options={}. Specifically I’m not sure  
> of its role in this given line of code:
> 
> helper file:
> 
> def render\_show\_actions(resource, actions, options={})  
> commands = actions.map do |action|  
> send “render\_show\_action\_#{action}”, resource, options  
> end
> 
> controller:
> 
> def render\_show\_action\_pass(student, options={})  
> @template.link\_to “pass”, pass\_student\_path(student), :method =\>  
> :get  
> end  
> helper\_method :render\_show\_action\_pass
> 
> It appears that the action in the helper dynamically builds the action  
> in controller. But then the question is what is options doing there and  
> which method is calling which?
> 
> Thanks for any suggestions.

def wow array=[1]  
array  
end

wow

copy and paste this in IRB and you’ll understand. “options={}” says that  
if no variable is passed for ‘options’, make it an empty hash.  
The rest of the code should make some more sense.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 3, 2010, 10:09pm UTC](https://www.ruby-forum.com/t/role-of-options/182852/3 "2010-02-03T22:09:06Z")

</div>

> if no variable is passed for ‘options’, make it an empty hash.  
> The rest of the code should make some more sense.

So if no parameter is passed in, why would it need to be there?

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 3, 2010, 10:22pm UTC](https://www.ruby-forum.com/t/role-of-options/182852/4 "2010-02-03T22:22:00Z")

</div>

Aldric G. wrote:

> John M. wrote:
> 
> > Hey all,
> > 
> > Can’t find a good explanation of options={}. Specifically I’m not sure  
> > of its role in this given line of code:
> > 
> > helper file:
> > 
> > def render\_show\_actions(resource, actions, options={})  
> > commands = actions.map do |action|  
> > send “render\_show\_action\_#{action}”, resource, options  
> > end
> > 
> > controller:
> > 
> > def render\_show\_action\_pass(student, options={})  
> > @template.link\_to “pass”, pass\_student\_path(student), :method =\>  
> > :get  
> > end  
> > helper\_method :render\_show\_action\_pass
> > 
> > It appears that the action in the helper dynamically builds the action  
> > in controller. But then the question is what is options doing there and  
> > which method is calling which?
> > 
> > Thanks for any suggestions.
> 
> def wow array=[1]  
> array  
> end
> 
> wow
> 
> copy and paste this in IRB and you’ll understand. “options={}” says that  
> if no variable is passed for ‘options’, make it an empty hash.  
> The rest of the code should make some more sense.

Yup. If you’re having trouble with this, please review your basic Ruby  
syntax.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 3, 2010, 10:20pm UTC](https://www.ruby-forum.com/t/role-of-options/182852/5 "2010-02-03T22:20:12Z")

</div>

It’s a default value.

In languages that don’t have default values, you _always_ have to pass  
in an  
argument. For example, if you defined it this way (or if Ruby didn’t  
support  
them…)

def render\_show\_actions(resource, actions, options)

When you called the function, you’d have to do this

render\_show\_actions @resource, [“action 1”, "action 2] , {}

instead of this

render\_show\_actions @resource, [“action 1”, "action 2]

… which is much nicer looking, and less typing.
