By Developers, For Developers

Historical errata for Text Processing with Ruby

PDF PgPaper PgTypeDescriptionFixed onComments
31,32ERROR

all source codes with echo commands and \
doesn’t work in bash
I think printf should be used instead of echo.

2015-05-24
iiiOK

Please, if possible, include a chapter on how to extract data from PDF files.
In linux and windows there are several utilities with dozens of options and integration with Ruby would be very interesting.

2015-06-28Hi Arnaldo, \n \nI had always intended to include a chapter on reading from PDFs, but in the end it seemed like there wasn't something useful to say about it. I can't talk about parsing the PDF format itself, since that wouldn't be practical and would take a whole book; but if I talk about using a library, that's not particularly interesting to the reader. In the end, I couldn't figure out a way to do it that justified its inclusion. \n \nBest, \nRob
8TYPO

“… each_line will result in a noticeably more solution.”

This phrase is missing a word.

2015-05-24
78TYPO

“Allows you to hone in on” - should be HOME in on

2015-06-23
24TYPO

There should not be a trailing forward slash after
www.example.com

(first line of output in the first example under the heading “Running the Script”)

2015-06-28
26TYPO

Spelling mistake in p.26 paragraph 2 “… searchs the filesystem…” : “searchs” should be “searches”

2015-06-28
32SUGGEST

When I run the first example on page 32 I don’t get precisely the same output.

Running zsh I get a trailing ‘%’ character after ‘bar’, indicating the text ended without a newline character, and running bash the command prompt starts immediately after the output (the ‘bar’ in this case).

My suggestion is that for this example and similar ones you might consider adding a newline character to the end of the input i.e. ‘foo\
bar\
’ in this case. This way the output on both bash and zsh would match the book (I think).

2015-06-28
144TYPO

Misspelling of matches

2015-08-20Sorry, I may be being completely blind but I don't see this. Could you give me some context for where the typo is? No worries if not — it should be caught in the copy-editing stage!
5TYPO

This sentence seems awkward and considering you have not mentioned a Log file before this line, it seems a bit off.

“One example of this sort of log file that you might have encountered before is from the popular web server Apache.”

2015-09-04
13SUGGEST

Maybe put an inline reference to an URL where one can get this data?

“The National Oceanic and Atmospheric Administration releases data ”

105TYPO

In the subsection “String-specific encodings” there’s an extra ‘the’ which should be removed.

The sentence reads as follows:

The result is a string encoded as ASCII that has had “the” any characters that are invalid ASCII stripped from it.

That “the” can be removed.

2015-09-04
105TYPO

Duplicated verb:

If we know that the string we have is is in a specific format,

2015-09-04
110TYPO

on section Detecting Encodings, a method is referred by the wrong name. The paragraph in question reads like so:

If we want to both detect the encoding and also change Ruby’s internal encoding of the string - […] we can do so with the detect_encoding method.

however that method is called detect_encoding! as can be seen on the follow-up example.

2015-09-04As discussed on the forum — both methods exist and do different things.
68SUGGEST

The one-liner for finding all home directories of all users can be slightly simplified. It currently reads as:

$ ruby -F: -ane ‘puts File.expand_path(“~” + $F[0]) unless /^#/’ /etc/passwd

However, the home directory is listed in passwd itself at the 5th position (6th if you’re counting starting at 1). That means the one-liner:

$ ruby -F: -ane ‘puts $F[5] unless /^#/’ /etc/passwd

is slightly shorter and equivalent, at least for systems which provide /etc/passwd with all fields. I did notice that the example User Database doesn’t show home directories, though.

2015-09-04My system does show home directories, and others might too, but in this case I wanted to show an example of doing some further processing with Ruby; it might be somewhat contrived as a result, but the important bit is seeing the use of Ruby to work with the data, I think.
36SUGGEST

the code from section “The Logic” can be slightly improved if we use join, instead of iterating over each url. Like below:

#!/usr/bin/env ruby

$stdin.each_line do |line|
urls = line.scan(${https?://\\S+})
puts urls.join(“\
”)
end

Wondering if I’m missing something as to why it couldn’t be done like above.

2015-09-04
36SUGGEST

$ cat foo | bar

can be written as:

$ bar < foo

2015-09-04I'm quite a strong believe in the supposedly unnecessary use of cat, since it preserves the left-to-right order of the pipeline that — especially for beginners — makes it much easier to read and understand.
238TYPO

If you want to pipe the contents of a file, you have two options. The first is to
use cat. The second is to have the shell pass the file as input, something we
can achieve with >: (should this symbol be pointing the other way or vice versa to match the example below?)

user@computer:~ $ cat Documents/birthday-list.txt | grep Ruby
Text Processing with Ruby by Rob Miller

user@computer:~ $ grep Ruby < Documents/birthday-list.txt
Text Processing with Ruby by Rob Miller

20SUGGEST

When having process ids with different digit numbers (ie 3003 and 12755), then using the cut with the space delimiter will not display the smaller number so by using the following:
ps ax | grep ruby | cut -d’ ’ -f1

we will get the following output:
(blank line here as process id is four digits)
12755
12770
28883

when using a different command like awk:
ps ax | grep ruby | awk ‘{print $1}’
we will get:

3003
12755
12770
28883

7ERROR

In Chapter 3 on the bottom page 7 (Streaming Lines with the -n Switch) of the free excerpt the text after the code snippet " $ printf “foo\
bar\
” | ruby -ne ‘puts $_.upcase’ " is not properly set due to a markup problem.

31ERROR

There seems to be some HTML mark up left at the bottom of the page (below the FOO/BAR)

$ printf “foo\
bar\
” | ruby -ne ‘puts $_.upcase’
FOO
BAR

There’s more to $_ than this, though.

134TYPO

In chapter 5 (delimited data) there are a couple of places where you refer to $; as a record separator and others as a field separator - also there are a few places where you say “record separator” when I think you mean “field separator”

6047TYPO

Second line of “Deleting Elements” section has “… is to to allow …”.

One to too many

99ERROR

In paragraph 2, ‘detect_encoding’ should be ‘detect_encoding!’ (i.e. with a ‘!’ suffix).

162ERROR

In nlp/simple-search.rb: the return value of ‘articles.select’ should be assigned to the variable ‘matches’, not ‘articles’.

225ERROR

In erb/purchase-ledger-decorators.erb: ‘Currency’ doesn’t match the class name defined in purchase-ledger-decorators.rb, which is ‘Dollars’.

[In the source code download, there’s an additional instance of this in the computation of ‘Total’, and there are two spurious ‘<%’ lines.]

85ERROR

The code example for LeagueTable#initialize is incorrect, as url as passed in as a method argument, but the first line uses url ||= 'default' without ever considering the method argument. Either the default value for the url should be a method argument default, and the code should use url = url, or the first line of the method should be @url = url || “default”.

138ERROR

The text and code are out of sync and in conflict.

- The text is talking about specifying the assignment token, but the code snippet starts with defining newline tokens, using “alternation” which isn’t discussed until pg 146… “In the delimiter definition, we’re introduced to another new concept: alternation…” The text on pg. 138 talks about the snippet as if this line doesn’t exist.

- The method `.absent?` is introduced without talking about it until page 146 in a different code snippet, which explicitly states “The .absent? method is something we haven’t seen before.”

- Further down the text refers to code that doesn’t exist:
“… the config file is made up of a name, then the assignment operator, then a value. We then allow for whitespace at the end of the line, followed by a newline character.”
The whitespace token is not accounted for in the code.
```
rule(:item) { name >> assignment >> value.as(:value) >> newline }
```
The reader is left trying to understand how the newline token is included between `value` and `newline` (it’s not). `whitespace`, was previously defined but only included in the definition for either side of `assignment`.

1313ERROR

The book incorrectly specifies that: “The first column, containing the date of the week in
question, is always twelve characters long.” - taken from the data found in: wksst8110.for.

There first column containing the date is actually just 10 characters long. This is also evident from the code sample “noaa-first- row-simple.rb”.

The line “file.read(10)” only reads 10 bytes - not 12 as suggested.

29TYPO

Third paragraph, last sentence reads: “At times like these, it would nice if we could introduce Ruby …”

I think it was meant to say, “At times like these, it would be nice …”

37SUGGEST

The return value is not given for the command to return the longest line from ` a\
bb`

I’m thinking it should be `bb`, but when I ran the command, it returned `a`.

My suggestion is to include the return value on the next print of the book, or append some `chomp`s on `$_` so that the expected `bb` is returned

14ERROR

In the “noaa-all-rows.rb”, to skip to the next line, you must file.read(2) instead of 1.

16ERROR

Seeking through the file. Must read 64 characters to skip a whole line.

17ERROR

In “noaa-seek.rb” / “get_week” block. to skip lines must multiply by 64 instead of 63.

Categories: