tassosss: Shen Wei Zhao Yunlan Era (Default)
tassosss ([personal profile] tassosss) wrote in [community profile] rubyonrails 2009-05-26 01:02 am (UTC)

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?

Post a comment in response:

If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting