简体   繁体   中英

New Ruby syntax doesn't work everytime

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..." %>
                     ^------------------------------^

The JSON style Hash syntax is for Hash es whose keys are Symbol s 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 ab mean the identifier ab or does it mean "subtract b from a , ie a.-(b) ?)

您试图将密钥设置为字符串而不是符号。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM