0

I would like to ask Ruby experts for explaination.

I can't use something like "foo-bar": true, but foobar: true works. Did anyone notice this?

This works:

In new Ruby syntax we can replace this hash rocket:

<%= f.submit "Save", :class => "btn" %>
                     ^-------------^

with this

<%= f.submit "Save", class: "btn" %>
                     ^----------^

This doesn't:

Now, how about symbols with two words? This doesn't work - syntax error!

<%= f.submit "Save", "data-disable-with": "Saving..." %>
                     ^------------------------------^
Stefan Huska
  • 547
  • 1
  • 7
  • 13
  • possible duplicate of [Is there any difference between the \`:key => "value"\` and \`key: "value"\` hash notations?](http://stackoverflow.com/questions/8675206/is-there-any-difference-between-the-key-value-and-key-value-hash-no) – mu is too short Nov 06 '13 at 20:29

2 Answers2

4

The JSON style Hash syntax is for Hashes whose keys are Symbols which are valid Ruby identifiers. Your key is neither a Symbol (it's a String) nor a valid Ruby identifier (Ruby identifiers cannot contain a hyphen, because that creates an ambiguity with the binary infix - operator: does a-b mean the identifier a-b or does it mean "subtract b from a, i.e. a.-(b)?)

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
-1

You are trying to set the key as a string instead of a symbol.

Timbinous
  • 2,863
  • 1
  • 16
  • 9