BernardK

BernardK

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.

First Post!

noelrappin

noelrappin

Author of Modern Front-End Development for Rails

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.

Where Next?

Popular Pragmatic Bookshelf topics Top

edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New
mcpierce
@mfazio23 I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1033 17470 383
New
AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
New
malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New
New

Sub Categories: