Need some clarification of a very small part of the Poignant Guide...?

A

Abe.Burnett

It's actually only a block that's doing something I didn't quite catch
as I was working through the previous four chapters.

Here's the block:

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

My question:
What exactly is the relationship between verb and the following array
which is followed by a period? What does the period do? I get the
feeling some sort of shortcut is taking place in those two lines but
I'm not sure what it is. Is there another way of putting it that will
help me see how it's working?

Thanks in advance!

Regards,

Abe
 
J

jeem

The period means that the next word, "each", is a method we're calling
on the array. (Or in more OOish terms, "each" is a message we're
sending to the array.) The line break makes it look a little more
confusing. It's the same as this:

verb = 'rescued'
more_verbs = ['sedated','sprinkled','electrocuted']
more_verbs.each do |verb|
puts "Dr. Cham " + verb + " his niece Hannah."
end
puts "Yes, Dr. Cham " + verb + " his niece Hannah."

The "each" method, like many in ruby, expects a block. The block is
the thing that starts with "do |verb|" and ends with the "end". When
you call "each", it calls the block for each of its elements, hence the
name, passing the element into the block as the argument. So the
"puts" within the block is called three times, with "verb" being
'sedated', 'sprinkled', and finally 'electrocuted'.

Jim
 
A

Abe.Burnett

AHA!

That clears it right up!

Then, verb is being set to 'rescued' for an arbitrary reason. I think
that was throwing me off a bit too: looking for where 'rescued' would
be used. As far as I can tell, and certainly in running the piece of
code, it doesn't seem to have any purpose whatsoever, other than just
giving verb an initial value.

Thanks for the concise response. :)

Regards,

Abe
 
E

Eric K Idema

Then, verb is being set to 'rescued' for an arbitrary reason. I think
that was throwing me off a bit too: looking for where 'rescued' would
be used. As far as I can tell, and certainly in running the piece of
code, it doesn't seem to have any purpose whatsoever, other than just
giving verb an initial value.

He sets verb to 'rescued' to illustrate the point that the block, somewhat
unexpectedly, will overwrite the value of verb. You are supposed to be
surprised when the last puts statement doesn't say he rescued his niece.

This behavior can lead to difficult to find bugs, and is something to be
aware of.

Eric
 
C

ChrisH

code, it doesn't seem to have any purpose whatsoever, other than just
giving verb an initial value.

Did you notice what the value of that variable was after the block had
executed?

I believe the purpose here is to show that if a variable is defined
before a block is called, any reference that variable in the block is a
refence to the existing variable, From the Programming Ruby book
"...if at the time the block executes a local variable
already exists with the same name as that of a
variable in the block, the existing local variable will
be used in the block".
This demonstrates that block vars do not shadow pre-existing vars in
scope with the same name.

Otherwise, any variable created in the block only exists in that
block's scope.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top