Ruby运算符优先级表

用户名

请给我看一个权威的,经过同行评审/维护的Ruby优先级表(由operatornon-operatorsModifys组成)。

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

1. http://phrogz.net/programmingruby/language.html#table_18.4 -的本书,其中文档的Ruby 1.6,它发行于2000年9月,并包括一个格式错误或拼写错误({被列为赋值运算符) 。

2. http://www.techotopia.com/index.php/Ruby_Operator_Precedence-上面的Pickaxe表的近似副本,包括错误的表{,并意外地描述||逻辑“ AND”

3. http://www.tutorialspoint.com/ruby/ruby_operators.htm-也是Pickaxe的近似副本,尽管它将||逻辑“ OR”的描述固定了,但仍{列为赋值运算符。同样,它列出::并错误地描述为一个恒定的分辨率操作者::不是操作者)。

4. http://my.safaribooksonline.com/book/web-development/ruby/9780596516178/expressions-and-operators/operators - Ruby编程语言的书,其中记录的Ruby1.81.9,这是在2003年发布2007年8月和十二月, 分别。这本书由David Flanagan和Yukihiro Matsumoto(又名Ruby的发明者“ Matz”)于2008年出版。它似乎是操作员,非操作员,修饰符和支持信息的最新,最准确的列表。顺便说一句,在2005年左右,与Ruby一起引起了人们的兴趣,Rails于2004年7月发布。

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

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

Ruby2.1.0是在圣诞节在发行的2013,并且类似地,没有列出任何操作员不兼容的地方。

因此,我决定根据Flanagan / Matz的书包含一个答案,并使其成为社区Wiki

用户名

Ruby 2.1.0、2.0、1.9、1.8

操作者是一个表示操作(如加法或比较)要在一个或多个操作数执行的令牌。操作数是表达式,并且运算符允许我们将这些操作数表达式组合成更大的表达式。参考

N = arity =操作员进行操作的操作数。参考

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

M =可定义性= Ruby将其许多运算符实现为方法,从而允许类为这些运算符定义新的含义。的列M指定哪些运算符是方法。标有a的运算符Y可以通过方法实现,并且可以重新定义,标有a的运算符N可以不重新定义参考

下表按降序排列(最高优先级在顶部)。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章