简体   繁体   English

新的Ruby语法并非每次都有效

[英]New Ruby syntax doesn't work everytime

I would like to ask Ruby experts for explaination. 我想请Ruby专家进行解释。

I can't use something like "foo-bar": true , but foobar: true works. 我不能使用"foo-bar": true ,但是foobar: true可以的。 Did anyone notice this? 有人注意到吗?

This works: 这有效:

In new Ruby syntax we can replace this hash rocket: 在新的Ruby语法中,我们可以替换此哈希火箭:

<%= 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. JSON样式Hash语法适用于Hash其键是Symbol ,它们是有效的Ruby标识符。 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) ?) 你的关键既不是一个Symbol (这是一个String ),也不是有效的Ruby标识符(Ruby的标识符不能包含一个连字符,因为它创建与二进制中缀的模糊性-运营商:不ab意味着标识符ab还是意味着“减去ba ,即a.-(b) ?)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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