向我展示一个权威的、经过同行评审/维护的Ruby优先表(operatorsnon-operatorsmodifiers).

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

1. http://phrogz.net/programmingruby/language.html#table_18.4——Pickaxe本书记录了Ruby 1.6,该书于2000年9月发布,其中包含一个格式错误或打字错误({被列为assignment运算符).

2. http://www.techotopia.com/index.php/Ruby_Operator_Precedence-上述Pickaxe表的近似副本,包括错误的{,意外地将||描述为Logical 'AND'.

3. http://www.tutorialspoint.com/ruby/ruby_operators.htm——也是Pickaxe表的一个近似副本,虽然它将||的描述修改为Logical 'OR',但它仍然将{列为assignment运算符.此外,它还列出了::,并错误地将其描述为constant resolution operator(::not个操作员).

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月发布.这本书由大卫·弗拉纳根和松本幸弘(又名"玛兹",Ruby的发明者)于2008年出版.它似乎是最新、最准确的操作员、非操作员、修改器和支持信息列表.顺便说一句,在2005年前后,人们对Ruby的兴趣与Rails同步飙升,Rails于2004年7月发布.

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

Ruby 2.0 was released in February 2013, and was intended to be fully backward compatible with Ruby 1.9.3.在为数不多的已知不兼容性中,没有一个与操作员有关.

Ruby 2.1.0 was released on Christmas Day in 2013,同样,没有列出任何运算符不兼容.

因此,我决定根据Flanagan/Matz的书写一个答案,并将其定为community wiki.

推荐答案

Ruby 2.1.0, 2.0, 1.9, 1.8

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

N=arity=运算符操作的操作数.(Ref)

A=associativity=同一运算符(或具有相同优先级的运算符)在表达式中顺序出现时的求值顺序.值L表示表达式从left to right计算.值R表示表达式从right to left计算.值N表示运算符为nonassociative,不能在没有括号的表达式中多次使用,以指定计算顺序.(Ref)

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

下表按降序排列(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相关问答推荐

ruby:比较两个单词并从中提取不常见的字母

定义Struct时如何指定成员的类型?

独立于 Rails 使用 HAML 文件中的布局

使用 RSpec 存根 Time.now

python:pythonbrew 和 virtualenv 有什么区别?

Ruby 是否提供了一种使用指定编码执行 File.read() 的方法?

方法应该以 (?问号)结尾吗?只返回一个布尔值?

拆分 Ruby 字符串时如何保留分隔符?

如果浮点组件不是 .00 sprintf/printf,则仅显示小数点

如何合并散列数组以获取值数组的散列

格式化 Ruby 的漂亮打印

在 Ruby 中将多个代码块作为参数传递

警告! PATH 设置不正确,通常这是由 shell 初始化文件引起的

如何设置方法测试中使用的私有实例变量?

文字数字中的下划线是什么意思?

为什么 Ruby 没有真正的 StringBuffer 或 StringIO?

处理来自 Net::HTTP 的异常的最佳方法是什么?

Ruby 方法to_sym有什么作用?

为什么表达式 (true == true == true) 会产生语法错误?

Ruby 中的动态方法调用