Precedence of **

Discussion in 'Ruby' started by Caleb Clausen, Sep 25, 2007.

  1. 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?
     
    Caleb Clausen, Sep 25, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Frauzel

    Operator precedence

    David Frauzel, May 13, 2004, in forum: Perl
    Replies:
    2
    Views:
    548
    Luc Van Hove
    May 17, 2004
  2. Replies:
    1
    Views:
    8,827
    Siva M
    Sep 15, 2005
  3. kbd

    Operator Precedence

    kbd, Jul 22, 2004, in forum: Java
    Replies:
    10
    Views:
    758
    Roedy Green
    Jul 27, 2004
  4. Replies:
    3
    Views:
    413
    Roedy Green
    Sep 13, 2005
  5. Andrew Koenig
    Replies:
    10
    Views:
    554
    Ron Natalie
    Jul 14, 2003
Loading...

Share This Page