Precedence of **

C

Caleb Clausen

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 '!i**2'
=> [:call, [:not, [:vcall, :i]], :**, [:array, [:lit, 2]]]
irb(main):003:0> ParseTree.translate '~i**2'
=> [:call, [:call, [:vcall, :i], :~], :**, [:array, [:lit, 2]]]
irb(main):004:0> ParseTree.translate '+i**2'
=> [:call, [:call, [:vcall, :i], :+@], :**, [:array, [:lit, 2]]]
irb(main):005:0> ParseTree.translate '-i**2'
=> [:call, [:call, [:vcall, :i], :**, [:array, [:lit, 2]]], :-@]

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top