ruby的方法调用优先级是多少
http://phrogz.net/programmingruby/language.html#table_18.4 上面链接提供的表格仅给出了 ruby 运算符的优先级。方法(或者我应该说:消息/函数)的优先级是什么?
例如,当我在 irb 中输入如下内容时,
Math.sqrt 2 + 2
我得到的结果是 2.0。如果没有明确的优先级规则,我就无法决定在哪里使用括号以及在哪里省略它们。所以,请有人帮助我摆脱这种不确定性。提前致谢!
http://phrogz.net/programmingruby/language.html#table_18.4
The table provided by the link above only gives the precedence of ruby's operators. What's the precedence of a method(or should I say: a message/ function) ?
For example, when I input something as below in irb
Math.sqrt 2 + 2
I got 2.0 as the result. Without the definite rules of the precedence, I just can't decide where to use the parens and where to omit them. So, someone please help me get rid of this uncertainty. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
任何运算符都优先于方法调用。
强烈建议使用
()
进行方法调用,以避免出现您所询问的情况。Any operator has precedence over the method call.
It is highly recommended to use
()
for method calls to avoid situations like the one you're asking about.Ruby 方法调用的优先级低于任何运算符,但这并不是全部 - 有一些边缘情况并不明显。你的问题让我对其中一个感到好奇,所以我在 ruby-talk 邮件列表上询问。您应该会发现生成的线程很有帮助。
另外,阅读这篇博客文章,以获得一个很好的论点,即您应该自由地使用括号,尤其是当您学习红宝石。
Ruby method invocation has a lower precedence than any of the operators, but that's not the full story - there are some edge cases that are nonobvious. Your question got me curious about one of them, so I asked on the ruby-talk mailing list. You should find the resulting thread helpful.
Also, read this blog post for a good argument that you should use parentheses liberally, especially while you are learning ruby.
我们可以使用您的《Ruby 编程》链接作为起点,并自行发现未加括号的方法调用属于优先级表中的位置。我只会展示确定位置的最终测试。
它低于
已定义?
:啊哈,它高于
未
:现在您确切地知道何时应该使用括号。
旁注
(感谢 Martin Demello 的 ruby-talk 链接提醒我)
请记住,表中的运算符仅在未在方法调用语法中使用时才适用。示例 - 以下内容不遵守
*
和+
的规则,因为它们不用作运算符,而是用作方法:如果这看起来令人困惑,那么同样的事情是发生在这里:
We can use your Programming Ruby link as a starting point and discover for ourselves where unparenthesized method-calls belong in the precedence table. I'm only going to show the final tests that pinpoint the location.
It's lower than
defined?
:Aha, it's higher than
not
:Now you know exactly when you should use parentheses.
Sidenote
(thanks to Martin Demello's link to ruby-talk for reminding me)
Keep in mind that the operators in the table only apply when not being used in the method-call syntax. Example - the following doesn't obey the rules for
*
and+
because they're not being used as operators, but methods:If that looks confusing, the same thing is happening here:
在 Ruby 中,任何运算符都优先于方法调用,例如首先计算运算符。
然而,
Math.sqrt 2 + 2
示例很好地说明了省略括号会多么难以阅读且不直观。人们可能期望在这里计算Math.sqrt(2) + 2
。当你遇到这样的一行时,你可能会想:编码员的意图是什么?这是一个错误吗?
尽可能使用括号总是一个好主意,以明确你想要什么,
特别是当存在方法调用或多个运算符时 - 这只是良好的风格和最低风险的方法(例如,不要做出假设,但通过使用括号使自己清楚)。
在表达式中添加额外的括号从来没有什么坏处,但将它们省略可能会造成很大的损失。
这是我最近遇到的一个很好的例子:
我希望这很好地说明了为什么使用括号很重要
另外:
在“调用方法”下检查此处:http://ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html
引用:
另请参阅:http://phrogz.net/programmingruby/language.html
In Ruby any operator has precedence over method calls, e.g. operators are evaluated first.
However the example
Math.sqrt 2 + 2
is a good illustration on how hard to read and unintuitive it can be to leave out parentheses. One might expectMath.sqrt(2) + 2
to be evaluated here.When you come across a line like this, you might think: What did the coder intend? Is this a bug?
It is always a good idea to use parentheses whenever you can, to make clear what you want,
especially when there is a method call or multiple operators - it's just good style, and the lowest risk approach (e.g. don't make assumptions, but make yourself clear by using parentheses).
It never hurts to add extra parentheses in expressions, but leaving them out can hurt quite a bit.
Here's a nice example which I recently came across:
I hope this nicely illustrates why it's important to use parentheses
Also:
Check here, under "Calling a Method" : http://ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html
Quote:
See also: http://phrogz.net/programmingruby/language.html