Why's (poignant) Guide to Ruby

I am reading “why’s (poignant) Guide to Ruby”, which is both
entertaining and an excellent introduction to Ruby. In Chapter 5, “Them
What Make the Rules and Them What Live the Dream”, I stumble on such a
obvious error that I start to question myself. Do I understand what he’s
trying to explain?

verb = ‘rescued’
[‘sedated’, ‘sprinkled’, ‘electrocuted’].
each do |verb|
puts “Dr. Cham " + verb + " his niece Hannah.”
end
puts “Yes, Dr. Cham " + verb + " his niece Hannah.”

OUTPUT
Dr. Cham sedated his niece Hannah.
Dr. Cham sprinkled his niece Hannah.
Dr. Cham electrocuted his niece Hannah.
Yes, Dr. Cham electrocuted his niece Hannah.

But I feel very strongly that the last line should be:

Yes, Dr. Cham rescued his niece Hannah.

This part of the book covers scoping issues in particular, so it strikes
me as odd that this mistake happens here. Most of the examples in the
book are rather acurate.

It would appear irb agrees with you. Unfortunately the author cannot be
contacted (does that mean he cannot then be contradicted?). But how nice
that such readable code is entirely executable, too bad it did not
prevent this error.

verb = ‘rescued’
=> “rescued”

[‘sedated’, ‘sprinkled’, ‘electrocuted’].
?> each do |verb|
?> puts “Dr. Cham " + verb + " his niece Hannah.”

end
Dr. Cham sedated his niece Hannah.
Dr. Cham sprinkled his niece Hannah.
Dr. Cham electrocuted his niece Hannah.
=> [“sedated”, “sprinkled”, “electrocuted”]

puts “Yes, Dr. Cham " + verb + " his niece Hannah.”
Yes, Dr. Cham rescued his niece Hannah.
=> nil

Now, let this be a lesson to all of us …

But I feel very strongly that the last line should be:

Yes, Dr. Cham rescued his niece Hannah.

Correct.

Agent M. wrote in post #1004694:

verb = ‘rescued’
[‘sedated’, ‘sprinkled’, ‘electrocuted’].
each do |verb|
puts “Dr. Cham " + verb + " his niece Hannah.”
end
puts “Yes, Dr. Cham " + verb + " his niece Hannah.”

OUTPUT
Dr. Cham sedated his niece Hannah.
Dr. Cham sprinkled his niece Hannah.
Dr. Cham electrocuted his niece Hannah.
Yes, Dr. Cham electrocuted his niece Hannah.

But I feel very strongly that the last line should be:

Yes, Dr. Cham rescued his niece Hannah.

This part of the book covers scoping issues in particular, so it strikes
me as odd that this mistake happens here. Most of the examples in the
book are rather acurate.

_why did not make a mistake:

puts RUBY_VERSION

verb = ‘rescued’

[‘sedated’, ‘sprinkled’, ‘electrocuted’].each do |verb|
puts “Dr. Cham " + verb + " his niece Hannah.”
end

puts “Yes, Dr. Cham " + verb + " his niece Hannah”

–output:–
1.8.6
Dr. Cham sedated his niece Hannah.
Dr. Cham sprinkled his niece Hannah.
Dr. Cham electrocuted his niece Hannah.
Yes, Dr. Cham electrocuted his niece Hannah

–output:–
1.9.2
Dr. Cham sedated his niece Hannah.
Dr. Cham sprinkled his niece Hannah.
Dr. Cham electrocuted his niece Hannah.
Yes, Dr. Cham rescued his niece Hannah

In ruby 1.8.6, when a block parameter variable has the same name as
another variable that the block can see, then the block parameter
variable is the other variable.

Because that “feature” was so universally hated, it was change in ruby
1.9.

Whoops.

I need it because I generate a complex data structure from more simple
primitives.

That’s beginning programming in any language.

Having the names around makes it easier to get the
desired results.

Wrong. All the time. In any language.