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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Aldric Giacomoni wrote:> John Merlino 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.