Okay. I think I have this but I just want to clarify how the |variable| works. The block is this (ps I love your examples!)
companions.each do |companion| puts companion.upcase + '!' end
What happens is that the .each takes the first name in the array and sends it to the block where the block says "you're my input and I'm going to now call you 'companion'." Then the block says "since you are a 'companion' I'm going to output you in all caps with an exclamation point." Now the block's job is done and the .each sends it the next item in the array.
Essentially the |companion| is only relevant for the block itself and once the array item leaves the block it's no longer associated with the 'companion' variable. Is that right?
(no subject)
Date: 2009-05-26 01:02 am (UTC)The block is this (ps I love your examples!)
companions.each do |companion|
puts companion.upcase + '!'
end
What happens is that the .each takes the first name in the array and sends it to the block where the block says "you're my input and I'm going to now call you 'companion'." Then the block says "since you are a 'companion' I'm going to output you in all caps with an exclamation point." Now the block's job is done and the .each sends it the next item in the array.
Essentially the |companion| is only relevant for the block itself and once the array item leaves the block it's no longer associated with the 'companion' variable. Is that right?