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 topics Top

mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
New
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New
leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
New
jonmac
The allprojects block listed on page 245 produces the following error when syncing gradle: “org.gradle.api.GradleScriptException: A prob...
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
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

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
foxtrottwist
Here’s our thread for the Keyboardio Atreus. It is a mechanical keyboard based on and a slight update of the original Atreus (Keyboardio ...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File &gt; New Rule: And select Deny, O...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
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

Latest in PragProg

View all threads ❯