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

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
simonpeter
When I try the command to create a pair of migration files I get an error. user=> (create-migration "guestbook") Execution error (Ill...
New
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
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
hazardco
On page 78 the following code appears: <%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
taguniversalmachine
Hi, I am getting an error I cannot figure out on my test. I have what I think is the exact code from the book, other than I changed “us...
New

Other popular topics Top

AstonJ
Or looking forward to? :nerd_face:
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File > New Rule: And select Deny, O...
New

Sub Categories: