Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are 2 keyword arguments being interpreted as 1?

Why does the following work to correct disable a radio button in a form:

<%= radio_button_tag 'set_creation_radio', 'set_editor', false, disabled: true %>

But this does not (note addition of keyword argument checked):

<%= radio_button_tag 'set_creation_radio', 'set_editor', checked: false, disabled: true %>

When using a breakpoint at this location, checked produces the following output in the first (i.e. working) instance:

0> checked
=> true

whereas the second seems to interpret both keywords as a single argument:

0> checked
=> {:checked=>true, :disabled=>true}

This is the source code for radio_button_tag and I do not understand why checked would be interpreted as a hash in my example:

      def radio_button_tag(name, value, checked = false, options = {})
        html_options = { "type" => "radio", "name" => name, "id" => "#{sanitize_to_id(name)}_#{sanitize_to_id(value)}", "value" => value }.update(options.stringify_keys)
        html_options["checked"] = "checked" if checked
        tag :input, html_options
      end
like image 624
Jacob Miller Avatar asked Sep 13 '25 21:09

Jacob Miller


1 Answers

There's no magic in calling a method in Ruby that says "oh, that's a hash, so I'll move that to the fourth argument". So in your second example you have three arguments:

  • 'set_creation_radio'
  • 'set_editor'
  • { checked: false, disabled: true }

So those are assigned to the first three parameters in the radio_button_tag method call, ie.

  • name = 'set_creation_radio'
  • value = 'set_editor'
  • checked = { checked: false, disabled: true }
like image 107
smathy Avatar answered Sep 16 '25 15:09

smathy