ruby中优先级和/或方法参数的优先级

时间:2022-04-27 23:17:53

Here are two tests:

这是两个测试:

if [1,2,3,4].include? 2 && nil.nil?
  puts :hello
end
#=>

and

if [1,2,3,4].include?(2) && nil.nil?
  puts :hello
end
#=> hello

The above tells me that && has higher precedence than method arguments so it logically ands 2 && nil.nil? which is true and passes that as an argument to include?.

上面告诉我&&的优先级高于方法参数,所以逻辑上和2 && nil.nil?这是真的,并将其作为参数传递给包括?

However, there is this test:

但是,有这个测试:

if [1,2,3,4].include? 2 and nil.nil?
  puts :hello
end
#=> hello

So this is telling me that method arguments and 'and' have the same precedence (or method args are higher than 'and') since it passed 2 to include? before it processed 'and'.

所以这告诉我方法参数和'和'具有相同的优先级(或方法args高于'和'),因为它传递2包含?在它处理'和'之前。

Note: I understand that && and and have different precedence. The question is not regarding this but regarding and or or vs the arguments to a ruby method.

注意:我理解&&和并且具有不同的优先级。问题不是关于这个问题,而是关于和/或反对ruby方法的论点。

I can't find documentation that affirms this. For instances, this doesn't mention method arguments at all: http://phrogz.net/programmingruby/language.html#table_18.4 or http://romhack.wikia.com/wiki/Ruby_operators.

我找不到证实这一点的文件。例如,这根本没有提到方法参数:http://phrogz.net/programmingruby/language.html#table_18.4或http://romhack.wikia.com/wiki/Ruby_operators。

Could anyone explain this behavior? Namely in that how does ruby know to pass values as arguments to a method vs. process operators?

有谁能解释这种行为?也就是说,ruby如何知道将值作为参数传递给方法与流程操作符?

2 个解决方案

#1


1  

As you said && and and have different precedence, however the explanation for the following example:

正如您所说的&&并且具有不同的优先级,但是对以下示例的解释如下:

if [1,2,3,4].include? 2 and nil.nil?
  puts :hello
end
#=> hello

is the binding strenght of the and as you can read here: Difference between "and" and && in Ruby?

是你的结合强度,正如你在这里可以看到的:Ruby中“和”和&&的区别?

This basically explains that 2 and nil.nil? will be evaluated as nil, however it will return 2 as can be seen in this example:

这基本上解释了2和nil.nil?将被评估为nil,但是它将返回2,如下例所示:

foo = :foo
bar = nil

a = foo and bar
# => nil
a
# => :foo

a = foo && bar
# => nil
a
# => nil

#2


0  

I've never seen any documentation about method argument precedence, but one rule of thumb I use when seeing method arguments is to mentally strip the whitespace wherever possible in the arguments and still have the same expression. This normally gives me the precedence:

我从来没有见过任何关于方法参数优先级的文档,但是我在查看方法参数时使用的一条经验法则是在参数中尽可能精神上剥离空格并且仍然具有相同的表达式。这通常给我优先权:

[1,2,3,4].include? 2&&nil.nil? is the same expression, but you cannot strip the whitespace in [1,2,3,4].include? 2 and nil.nil? and therefore, the precedence is left to right ... I.e. Method argument is 2.

[1,2,3,4] .INCLUDE? 2 && nil.nil?是同一个表达式,但你不能去除[1,2,3,4]中的空格.include? 2和nil.nil?因此,优先权是从左到右...... I.e。方法参数是2。

Anyway, the better question is why on earth would you write statements like this?

无论如何,更好的问题是为什么你会写这样的陈述?

Omitting method parenthesis is only useful for code readability. However, your statements are hardly readable and makes one pause over the code and think about it more than he should. If I was to review code like this, I would definitely fail the code review due to poor readability.

省略方法括号仅对代码可读性有用。但是,您的陈述几乎不可读,并且会使代码暂停,并且比他应该更多地考虑它。如果我要审查这样的代码,由于可读性差,我肯定会无法通过代码审查。

In fact, many style guides explicitly state that most methods with arguments should be parenthesized (is this even a word ;). For example: Ruby style guide

实际上,许多样式指南明确指出大多数带参数的方法都应该用括号括起来(这甚至是一个单词;)。例如:Ruby样式指南

#1


1  

As you said && and and have different precedence, however the explanation for the following example:

正如您所说的&&并且具有不同的优先级,但是对以下示例的解释如下:

if [1,2,3,4].include? 2 and nil.nil?
  puts :hello
end
#=> hello

is the binding strenght of the and as you can read here: Difference between "and" and && in Ruby?

是你的结合强度,正如你在这里可以看到的:Ruby中“和”和&&的区别?

This basically explains that 2 and nil.nil? will be evaluated as nil, however it will return 2 as can be seen in this example:

这基本上解释了2和nil.nil?将被评估为nil,但是它将返回2,如下例所示:

foo = :foo
bar = nil

a = foo and bar
# => nil
a
# => :foo

a = foo && bar
# => nil
a
# => nil

#2


0  

I've never seen any documentation about method argument precedence, but one rule of thumb I use when seeing method arguments is to mentally strip the whitespace wherever possible in the arguments and still have the same expression. This normally gives me the precedence:

我从来没有见过任何关于方法参数优先级的文档,但是我在查看方法参数时使用的一条经验法则是在参数中尽可能精神上剥离空格并且仍然具有相同的表达式。这通常给我优先权:

[1,2,3,4].include? 2&&nil.nil? is the same expression, but you cannot strip the whitespace in [1,2,3,4].include? 2 and nil.nil? and therefore, the precedence is left to right ... I.e. Method argument is 2.

[1,2,3,4] .INCLUDE? 2 && nil.nil?是同一个表达式,但你不能去除[1,2,3,4]中的空格.include? 2和nil.nil?因此,优先权是从左到右...... I.e。方法参数是2。

Anyway, the better question is why on earth would you write statements like this?

无论如何,更好的问题是为什么你会写这样的陈述?

Omitting method parenthesis is only useful for code readability. However, your statements are hardly readable and makes one pause over the code and think about it more than he should. If I was to review code like this, I would definitely fail the code review due to poor readability.

省略方法括号仅对代码可读性有用。但是,您的陈述几乎不可读,并且会使代码暂停,并且比他应该更多地考虑它。如果我要审查这样的代码,由于可读性差,我肯定会无法通过代码审查。

In fact, many style guides explicitly state that most methods with arguments should be parenthesized (is this even a word ;). For example: Ruby style guide

实际上,许多样式指南明确指出大多数带参数的方法都应该用括号括起来(这甚至是一个单词;)。例如:Ruby样式指南