ruby的方法调用的优先顺序是什么 [英] What's the precedence of ruby's method call

查看:51
本文介绍了ruby的方法调用的优先顺序是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://phrogz.net/programmingruby/language.html#table_18.4 上面链接提供的表仅给出了ruby运算符的优先级.方法的优先级是什么(或者我应该说:消息/函数)?

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

例如,当我在irb中输入以下内容时

For example, when I input something as below in irb

Math.sqrt 2 + 2

我得到了2.0的结果.如果没有明确的优先规则,我就无法决定在哪里使用括号和在哪里省略它们.因此,请有人帮助我摆脱这种不确定性.预先感谢!

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!

推荐答案

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

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的方法调用的优先顺序是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆