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 ...
          -----> ^^^

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!