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

Popular Prag Prog topics Top

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
kuroneko
Whilst the author has been careful to provide exact results for the tests elsewhere in the book (such as surds with the transformation te...
New
iPaul
page 37 ANTLRInputStream input = new ANTLRInputStream(is); as of ANTLR 4 .8 should be: CharStream stream = CharStreams.fromStream(i...
New
Razor54672
The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “&gt;...
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
jonmac
The allprojects block listed on page 245 produces the following error when syncing gradle: “org.gradle.api.GradleScriptException: A prob...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New

Other popular topics Top

AstonJ
Or looking forward to? :nerd_face:
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
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
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
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: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
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
NewsBot
A new Clojure blog post/announcement has been posted! Get the full details here: https://clojure.org/news/2025/04/17/deref
New
New

Latest in PragProg

View all threads ❯