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!

Where Next?

Popular Pragmatic Bookshelf topics Top

iPaul
page 37 ANTLRInputStream input = new ANTLRInputStream(is); as of ANTLR 4 .8 should be: CharStream stream = CharStreams.fromStream(i...
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
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
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
conradwt
First, the code resources: Page 237: rumbl_umbrella/apps/rumbl/mix.exs Note: That this file is missing. Page 238: rumbl_umbrella/app...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
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
New
mcpierce
@mfazio23 I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
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
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New

Sub Categories: