BernardK

BernardK

Programming Ruby 3.2 (5th Edition): B1.0 pages 49, 54, 55, 58-59, 62, 63, 64

@noelrappin

+++++ page 49 Reopening Classes

While ...
the most unique features of Ruby’s class structure: The ability to ...
                                             -----> ^

In French I wouldn’t put a capital letter after a colon.
(Note : I have to use code block instead of quote to keep the arrow and caret aligned with the typo. )

+++++ page 54, paragraph 7 (counting the diagram for one)

You can also index arrays with a pair of numbers, [_start_, _count_]. This returns a new array
consisting of references to count number of objects starting at position start:
-----> should be            _count_ number ...               at position _start_:

(To be coherent _start_, and _count_ should be used both in the square brackets and in the next line.)

+++++ page 55, paragraph 5, line 2 :

replaced by cat. In the next line, the subarray [2, 0] is of length 0, so dog just is inserted at

-----> dog just is sounds strange to me, I would expect : so dog is just inserted at

+++++ page 55, paragraph 6, line 1 : superfluous what

It’s common to create arrays of short words, but that can be a pain, what with all the quotes
                                                              -----> ^^^^ <----- seems superfluous

SUGGESTION
+++++ pages 58-59 : why ’ in the regexp ?

page 58 bottom : the method words_from_string returns : string.downcase.scan(/[\w']+/)

Why an apostrophe in the regexp ([\w']) if the sentence does not contain any (p.59, par.3) :

p words_from_string(“I like Ruby, it is (usually) optimized for programmer happiness”)

In the previous edition (2010), it was :

p words_from_string(“But I didn’t inhale, he said (emphatically)”)

-----> So you could either remove the apostrophe from the regexp (stop ! it breaks the test assert_equal(["the", "cat's", "mat"] ...), or introduce one in the text, for example :

p words_from_string(“I like Ruby, it is (usually) optimized for programmer happiness, isn’t it ? <—”)

and

raw_text = "The problem breaks down into two parts. First, given some text
as a string, return a list of words. That sounds like an array, isn’t it ? <-----

+++++ page 62 : Blocks and Enumeration

In our program that wrote out the results of our word frequency analysis, we had the following loop:

top_five.each do |I|
^^word = top_five[i][0]
^^count = top_five[i][1]
^^puts “#{word}: #{count}”
end

(carets added for indentation)
-----> But this code exists nowhere in this new version. It is a remnant of old versions, for example :

programming-ruby-1-9_p4_.pdf

ISBN-10: 1-934356-08-5
ISBN-13: 978-1-934356-08-1
4.0 printing, May 2011
Version: 2011-5-11

→ second halh of the page 68 :

Download tut_containers/word_freq/ugly_word_count.rb

require_relative "words_from_string.rb"
require_relative "count_frequency.rb"

raw_text = %{
The problem breaks down into two parts. First, given some text as a
string, return a list of words. That sounds like an array. Then, build a
count for each distinct word. That sounds like a use for a hash---we can
index it with the word and use the corresponding entry to keep a count.}

word_list = words_from_string(raw_text)
counts    = count_frequency(word_list)
sorted    = counts.sort_by {|word, count| count}
top_five  = sorted.last(5)

for i in 0...5            # (this is ugly code--read on
  word = top_five[i][0]   # for a better version)
  count = top_five[i][1]
  puts "#{word}: #{count}"
end

-----> caution, page 63, line 4 :

A Ruby programmer might use a different enumerator method called map to write this code more compactly.

this code is not in sync with the old for loop.

=====> I have a solution :

in tut_containers/word_freq/better_word_count.rb on page 62 replace :

top_five.reverse_each do |word, count|

by :

top_five.reverse.each do |word, count|

The result is the same. Then : (carets before puts added for indentation)

Blocks and Enumeration

In our program that wrote out the results of our word frequency analysis, we had the following loop:

top_five.reverse.each do |word, count|
^^puts “#{word}: #{count}”
end

→ Then the replacement of each by map on page 63 perfectly corresponds to

puts top_five.reverse.map { |word, count| "#{word}: #{count}" }

in /best_word_count.rb, and that’s it.

+++++ page 63, paragraph after /best_word_count.rb : it ?, of of

The map method is now taking each element of our top five array and converting it to a new
                                                                        -----> ^^ <----them ???

-----> I would replace it by them (the map method is taking … and converting them (the elements)), or put a comma after top five array :

The map method is now taking each element of our top five array, and converting it [the array] …

-----> + next line (twice of) :

array made of of the strings that come as the result of executing the block.
    -----> ^^^^^

+++++ page 64, paragraph 3. I had difficulty to understand this sentence :

All tap does is …, and then return the original receiver of the method (which, from the perspective of the method pipeline does nothing

-----> it would help to add tap :

                        [line continued] —the receiver
calls the method tap and then the same object is returned ...
          -----> ^^^

First Post!

noelrappin

noelrappin

Author of Modern Front-End Development for Rails

P 49 – fixed
P 54 – that’s a formatting error, I think – _start_ should be indicating underlines.
P 55 – switched
P 55 – It’s not superfluous, it’s there as part of what I guess I’d call an idiom, and gives the sentence a different rhythm
P 58-59 – The apostrophe is in the regex because it was needed for the 2010 example (which I’ve changed). But I don’t think it’s hurting anything to keep it there?
P 62 – Yeah, I noticed that right after the beta went out. It’s been fixed – the code example was changed a couple of times to bring it to 2023 standards (so, no for loop…) and the text lagged behind the code in this case.
P 63 – I think what “this” is referring to is ambiguous, I’ll clarify. reverse_each is part of aligning the code with Standard Ruby style rules, so I’d prefer not to change it.
P 64 – Yes that would probably be clearer.

Thanks!

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
brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
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 trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
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
dtonhofer
@parrt In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New

Other popular topics Top

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
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
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
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1134 25456 753
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New