ruby的方法调用优先级是多少

发布于 2024-12-09 02:09:55 字数 360 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

素年丶 2024-12-16 02:09:56

任何运算符都优先于方法调用。
强烈建议使用 () 进行方法调用,以避免出现您所询问的情况。

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.

溺渁∝ 2024-12-16 02:09:56

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.

画▽骨i 2024-12-16 02:09:56

我们可以使用您的《Ruby 编程》链接作为起点,并自行发现未加括号的方法调用属于优先级表中的位置。我只会展示确定位置的最终测试。

它低于已定义?

defined? Math.sqrt 2
# => SyntaxError: unexpected tINTEGER, expecting end-of-input

啊哈,它高于

not Math.sqrt 2
=> false

现在您确切地知道何时应该使用括号。

旁注

(感谢 Martin Demello 的 ruby​​-talk 链接提醒我)

请记住,表中的运算符仅在未在方法调用语法中使用时才适用。示例 - 以下内容不遵守 *+ 的规则,因为它们不用作运算符,而是用作方法:

# This happens because unparenthesized method calls are right-associative.
2.* 5.+ 2  # => 14

如果这看起来令人困惑,那么同样的事情是发生在这里:

# The `modulo` is evaluated first because it's on the right
Math.sqrt 9.modulo 5

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?:

defined? Math.sqrt 2
# => SyntaxError: unexpected tINTEGER, expecting end-of-input

Aha, it's higher than not:

not Math.sqrt 2
=> false

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:

# This happens because unparenthesized method calls are right-associative.
2.* 5.+ 2  # => 14

If that looks confusing, the same thing is happening here:

# The `modulo` is evaluated first because it's on the right
Math.sqrt 9.modulo 5
属性 2024-12-16 02:09:56

在 Ruby 中,任何运算符都优先于方法调用,例如首先计算运算符。

然而,Math.sqrt 2 + 2 示例很好地说明了省略括号会多么难以阅读且不直观。人们可能期望在这里计算 Math.sqrt(2) + 2

当你遇到这样的一行时,你可能会想:编码员的意图是什么?这是一个错误吗?

尽可能使用括号总是一个好主意,以明确你想要什么,
特别是当存在方法调用或多个运算符时 - 这只是良好的风格和最低风险的方法(例如,不要做出假设,但通过使用括号使自己清楚)。

在表达式中添加额外的括号从来没有什么坏处,但将它们省略可能会造成很大的损失。

这是我最近遇到的一个很好的例子:

  def foo(arg)
    raise "error"
  end
  x = foo 4 rescue 7
  x 
    => nil    # oops!

  x = foo(4) rescue 7
  x
    => 7

我希望这很好地说明了为什么使用括号很重要

另外:

在“调用方法”下检查此处:http://ruby-doc.org/docs/ProgrammingRuby/html/tut_methods.html
引用:

[...]如果没有
歧义时,您可以省略参数列表周围的括号
调用方法。[...] 但是,除了最简单的情况外,我们不会
推荐这个
——有一些微妙的问题可能会绊倒你
up.[特别是,您必须在方法调用上使用括号,即
本身是另一个方法调用的参数(除非它是最后一个方法)
参数)。] 我们的规则很简单:如果有任何疑问,请使用
括号。

另请参阅: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 expect Math.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:

  def foo(arg)
    raise "error"
  end
  x = foo 4 rescue 7
  x 
    => nil    # oops!

  x = foo(4) rescue 7
  x
    => 7

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:

[...] If there is no
ambiguity you can omit the parentheses around the argument list when
calling a method.[...] However, except in the simplest cases we don't
recommend this
---there are some subtle problems that can trip you
up.[In particular, you must use parentheses on a method call that is
itself a parameter to another method call (unless it is the last
parameter).] Our rule is simple: if there's any doubt, use
parentheses.

See also: http://phrogz.net/programmingruby/language.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文