Role of options={}?

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.

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.

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?

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
[email protected]

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.