Ruby 运算符优先级表 [英] Ruby operator precedence table

查看:62
本文介绍了Ruby 运算符优先级表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向我展示一个权威的、经过同行评审/维护的 Ruby 优先级表(运算符非运算符修饰符).

Show me a definitive, peer-reviewed/maintained Ruby precedence table (of operators, non-operators, and modifiers).

多年来,我不得不依赖以下来源获取这些信息:

Over the years I have had to rely on the following sources for this information:

1. http://phrogz.net/programmingruby/language.html#table_18.4 - Pickaxe 书,记录了 Ruby 1.6,于 2000 年 9 月发布,包括格式错误或拼写错误({ 被列为赋值运算符).

1. http://phrogz.net/programmingruby/language.html#table_18.4 - The Pickaxe book, which documents Ruby 1.6, which was released in September 2000, and includes a formatting error or typo ({ is listed as an assignment operator).

2. http://www.techotopia.com/index.php/Ruby_Operator_Precedence - 上述 Pickaxe 表的近似副本,包括错误的 {,并且不小心将 || 描述为逻辑与".

2. http://www.techotopia.com/index.php/Ruby_Operator_Precedence - A near copy of the above Pickaxe table, including the erroneous {, and accidentally describes || as Logical 'AND'.

3. http://www.tutorialspoint.com/ruby/ruby_operators.htm - 也是 Pickaxe 表的近似副本,尽管它将 || 的描述修复为 Logical 'OR',但它仍然将 { 列为 赋值 运算符.同样,它列出了 :: 并错误地将其描述为 常量解析运算符(::not运算符).

3. http://www.tutorialspoint.com/ruby/ruby_operators.htm - Also a near copy of the Pickaxe table, though it fixes the description of || to Logical 'OR', yet it still lists { as an assignment operator. As well, it lists :: and incorrectly describes it as a constant resolution operator (:: is not an operator).

4. http://my.safaribooksonline.com/book/web-development/ruby/9780596516178/expressions-and-operators/operators - The Ruby Programming Language 一书,其中记录了 Ruby 1.81.9,分别于 2003 年 8 月和 2007 年 12 月发布.这本书由 David Flanagan 和 Yukihiro Matsumoto(又名Matz",Ruby 的发明者)于 2008 年出版.它似乎是最新和最准确的运营商、非运营商、修饰符和支持信息列表.顺便提一下,在 2005 年左右,随着 Rails 于 2004 年 7 月发布,人们对 Ruby 的兴趣激增.

4. http://my.safaribooksonline.com/book/web-development/ruby/9780596516178/expressions-and-operators/operators - The Ruby Programming Language book, which documents Ruby 1.8 and 1.9, which were released in August 2003 and December 2007, respectively. This book was published in 2008 by David Flanagan and Yukihiro Matsumoto (aka "Matz", the inventor of Ruby). It seems to be the most up-to-date and accurate list of operators, non-operators, modifiers, and supporting information. Incidentally, around 2005, interest in Ruby surged in tandem with Rails, which was released in July 2004.

5. http://romhack.wikia.com/wiki/Ruby_operators - 还记录了 Ruby 1.9 中的运算符,并在其表中包含非运算符和修饰符.

5. http://romhack.wikia.com/wiki/Ruby_operators - Also documents operators in Ruby 1.9, and includes non-operators and modifiers in its table.

Ruby 2.0 于 2013 年 2 月发布,旨在与 Ruby 1.9.3 完全向后兼容.在少数已知的不兼容性中,没有一个与运算符有关.

Ruby 2.0 was released in February 2013, and was intended to be fully backward compatible with Ruby 1.9.3. Of the few known incompatibilities, none are related to operators.

Ruby2.1.02013 的圣诞节发布,同样没有列出操作符不兼容问题.

Ruby 2.1.0 was released on Christmas Day in 2013, and similarly, no operator incompatibilities are listed.

因此,我决定包含一个基于 Flanagan/Matz 书籍的答案,并将其设为社区维基.

Thus, I decided to include an answer, based on the Flanagan/Matz book, and made it a community wiki.

推荐答案

Ruby 2.1.0, 2.0, 1.9, 1.8

operator 是一种标记,表示要对一个或多个操作数执行的操作(例如加法或比较).操作数是表达式,运算符允许我们将这些操作数表达式组合成更大的表达式.(参考)

Ruby 2.1.0, 2.0, 1.9, 1.8

An operator is a token that represents an operation (such as addition or comparison) to be performed on one or more operands. The operands are expressions, and operators allow us to combine these operand expressions into larger expressions. (Ref)

N = arity = 运算符操作的操作数的数量.(参考)

N = arity = The number of operands the operator operates on. (Ref)

A = associativity = 当相同的运算符(或具有相同优先级的运算符)按顺序出现在表达式中时的求值顺序.值 L 表示从 从左到右计算表达式.值 R 表示从 从右到左计算表达式.并且值 N 表示该运算符是非关联,并且不能在没有括号的表达式中多次使用以指定计算顺序.(参考)

A = associativity = The order of evaluation when the same operator (or operators with the same precedence) appear sequentially in an expression. The value L means that expressions are evaluated from left to right. The value R means that expressions are evaluated from right to left. And the value N means that the operator is nonassociative and cannot be used multiple times in an expression without parentheses to specify the evaluation order. (Ref)

M = 可定义性 = Ruby 将其许多运算符实现为方法,允许类为这些运算符定义新含义.列 M 指定哪些运算符是方法.用Y 标记的操作符是用方法实现的,可以重新定义,而用N 标记的操作符不能.(参考)

M = definability = Ruby implements a number of its operators as methods, allowing classes to define new meanings for those operators. Column M of specifies which operators are methods. Operators marked with a Y are implemented with methods and may be redefined, and operators marked with an N may not. (Ref)

下表按优先级降序排列(最高优先级).

The following table is ordered according to descending precedence (highest precedence at the top).

N A M  Operator(s)            Description
- - -  -----------            -----------
1 R Y  ! ~ +                  boolean NOT, bitwise complement, unary plus
                              (unary plus may be redefined from Ruby 1.9 with +@)

2 R Y  **                     exponentiation
1 R Y  -                      unary minus (redefine with -@)

2 L Y  * / %                  multiplication, division, modulo (remainder)
2 L Y  + -                    addition (or concatenation), subtraction

2 L Y  << >>                  bitwise shift-left (or append), bitwise shift-right
2 L Y  &                      bitwise AND

2 L Y  | ^                    bitwise OR, bitwise XOR (exclusive OR)
2 L Y  < <= >= >              ordering

2 N Y  == === != =~ !~ <=>    equality, pattern matching, comparison
                              (!= and !~ may not be redefined prior to Ruby 1.9)

2 L N  &&                     boolean AND
2 L N  ||                     boolean OR

2 N N  .. ...                 range creation (inclusive and exclusive)
                              and boolean flip-flops

3 R N  ? :                    ternary if-then-else (conditional)
2 L N  rescue                 exception-handling modifier

2 R N  =                      assignment
2 R N  **= *= /= %= += -=     assignment
2 R N  <<= >>=                assignment
2 R N  &&= &= ||= |= ^=       assignment

1 N N  defined?               test variable definition and type
1 R N  not                    boolean NOT (low precedence)
2 L N  and or                 boolean AND, boolean OR (low precedence)
2 N N  if unless while until  conditional and loop modifiers

这篇关于Ruby 运算符优先级表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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