By Developers, For Developers

Historical errata for Mastering Ruby Closures

PDF PgPaper PgTypeDescriptionFixed onComments
6TYPO

In the line 5 code example of lambda_counter.rb, I think it should be decr = lambda { x -= 1 } instead of decr = lambda { x += 1 }.

Good catch! You're absolutely right.
51TYPO

See amp; where should be & in symbol to prod example

59TYPO

Exercise 2 should say to instantiate classes. The ‘to’ is missing

59TYPO

Extra periods in exercise 2 at end of first sentence

30ERROR

I don’t think your implementation of File#open will work as expected if the caller doesn’t pass in a block. The “ensure” clause gets executed regardless, so the method will return a file which has already been closed.

I believe this version works correctly.

class File
def self.open(name, mode)
file = new(name, mode)
return file unless block_given?
begin
yield(file)
ensure
file.close
end
end
end

4TYPO

The result of
inner_lambda.call
should be
“I will not drive the principal’s car”

9TYPO

last para: “So yes, a lambda can most definitely be passed into a lambda.” should be “So yes, a lambda can most definitely be passed into a method.”

10SUGGEST

para 3: the point of your section “First-Class Values” is lost here when you suddenly refer to them as “first-class functions”. Do you mean they are values which happen to be functions? Why then is it that “Ruby’s methods are not first-class functions”?

36ERROR

In the code example “blocks/object_init_revised”, outputing “client.consumer_key” and “client.consumer_token” returns “nil”, unlike what the example shows.

The example should be instead: \n \nclient = Twitter::REST::Client.new({consumer_key: "YOUR_CONSUMER_KEY"}) do \n self.consumer_secret = "YOUR_CONSUMER_SECRET" \n self.access_token_secret = "YOUR_ACCESS_SECRET" \n self.access_token = "YOUR_ACCESS_TOKEN" \nend \n
72TYPO

In the solution of the 5th exercise of Chapter 1, you wrote :

reducer.call([], lambda { |acc, x| acc << x * 2}, [1,2,3,4,5])

but the second parameter is not the binary_function in the reducer example of the book. You should use the third argument as binary_function instead. This works:

reducer.call([], [1,2,3,4,5], lambda { |acc, x| acc << x * 2})

22SUGGEST

one of the last example in the page explain how to pass a variable to a block without being able to modify it inside the block:

x = “outside x”
1.times { |;x| x = “modified from the outside block” }
puts x # => “outside x”

From what is written it seems like (at least to me) that you can access the outside x and if you try to modify it inside the block it will not have any effect to the outside one.
But you have no access to the outside x into the block, that x is a completely new object which is nil in this case, so it probably makes no sense to pass it to the block.

48ERROR

first para: When Ruby sees an & and an object—any object—it will try to turn it into a block.

s/block/proc/ ?

4ERROR

The book shows:

>> inner_lambda.call()
=> “drive the principal’s car”

but inner_lamda.call() returns “I will not drive the principal’s car”

24SUGGEST

For the next edition, in code for blocks/fixnum_times.rb, use “Integer” since Fixnum is deprecated.

32ERROR

If the class Router definition is after the block I get the following error:

ruby router.rb
Traceback (most recent call last):
router.rb:1:in `

’: uninitialized constant Router (NameError)

Suggest putting the class definition before the usage call.

32-35SUGGEST

In the block call “routes = Router. new do”, on pages 32 and 33 there are two routes to match. On pages 34 and 35 there’s just one. Might be better to make them look the same.

45ERROR

On this page, the proc p is defined like this:

p = proc { |x, y| puts “x: #{x}, y: #{y}” }

One example shows the result of calling p with 3 arguments like this:

call(‘Ohai’, ‘Gentle’, ‘Reader’)

The book erroneously indicates that the result is:

x: Ohai, y: Gentle Reader

But the result is actually:

x: Ohai, y: Gentle

since the third argument is silently discarded.

6TYPO

re line 5 in the closures/lambda_counter.rb code snippet: ````` decr = lambda { x += 1 }`

I assume the ` +` is intended to be a ` -`

Categories: