Programming Ruby 3.2 (5th Edition): B2.0 page 455, completing Table 13—Ruby operators

@noelrappin

page 455, what about completing Table 13—Ruby operators with :: and . ?

I was surprised not to find the scope resolution operator and the method access operator. A long search in the Internet shows either copies of the Pickaxe, or of O’Reilly’s book, The Ruby Programming Language, or various personal interpretations. Already in 2014 somebody complained that is was difficult to find a reliable Ruby operator precedence table.

I would place the method invocation operator . between [] and **.

a = [1, 2, 3]
print 'a = ', a; puts
puts "a[-1].succ = #{a[-1].succ}"
puts "3 ** 2.succ = #{3 ** 2.succ}"
puts "1 + 2.succ = #{1 + 2.succ}"

produces

a = [1, 2, 3]
a[-1].succ = 4
3 ** 2.succ = 27
1 + 2.succ = 4

I don’t know where to place the scope resolution operator which is special in the sense that it only connects an expression returning a module name and another constant (page 265) :

The thing to the left must be a class or module, and the thing to the right is a constant defined in that class or module.

O’Reilly’s book page 89 :

In addition to simple references like this one, constant references can also be compound expressions. In this case, :: is used to separate the name of the constant from the class or module in which it is defined. The lefthand side of the :: may be an arbitrary expression that evaluates to a class or module object. (Usually, however, this expression is a simple constant reference that just names the class or module.) The righthand side of the :: is the name of a constant defined by the class or module.

The canonical source for Ruby operator precedence is precedence - Documentation for Ruby 3.3 – that doesn’t have scope or method access (probably because the parser doesn’t treat them like operators). The canonical source doesn’t have [] either, because that’s really just a method call.

I confess to being a little unsure what to do – I think the table should match the official docs, but there’s more to how the Ruby parser works than operator precedence.