Precedence of **

I recently discovered that the precedence of ** seems to be at
variance with the documentation. All tables of precedence for Ruby
that I have found (including in the pickaxe) have listed ** as just
above that of the four unary operators + - ! and ~. However, ruby
actually parses 3 of these as higher precedence than **. This is most
obvious when using ParseTree:

$ irb -rubygems
irb(main):001:0> require ‘parse_tree’
WARNING: overridding bool on
/usr/lib/ruby/gems/1.8/gems/ParseTree-2.0.2/lib/parse_tree.rb:244
=> true
irb(main):002:0> ParseTree.translate '!i2’
=> [:call, [:not, [:vcall, :i]], :
, [:array, [:lit, 2]]]
irb(main):003:0> ParseTree.translate '~i2’
=> [:call, [:call, [:vcall, :i], :~], :
, [:array, [:lit, 2]]]
irb(main):004:0> ParseTree.translate '+i2’
=> [:call, [:call, [:vcall, :i], :[email protected]], :
, [:array, [:lit, 2]]]
irb(main):005:0> ParseTree.translate '-i2’
=> [:call, [:call, [:vcall, :i], :
, [:array, [:lit, 2]]], :[email protected]]

Unary minus actually comes out as lower precedence than **, but the
other three unary ops all bind more tightly. I have seen this using
1.8.2 on Debian, and 1.8.5 and 1.8.6 on Ubuntu.

I see now that the table of precedence in parse.y actually has +, ~,
and ! as lower precedence than **. I thought it was going to be
something less obvious… Was it intended to be this way, and the
documentation is wrong?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs