From: dblack@... Date: 2006-02-28T23:06:23+09:00 Subject: Re: Same variable name used in block and local question Hi -- On Tue, 28 Feb 2006, rubyrus wrote: > Can anybody help me here, TIA!! > > This is the code. I expect to see the string "Afterward:turkey", but > why am I getting "Afterward:camel"? |animal| parameter in a block > should be temporary inside the block, right?? > > > Code: > ---------- > animal = "turkey" > > puts "Original:" + animal > puts > > ["cow","camel"].each { |animal| puts "in block:" + animal } > > puts > puts "Afterwards:" + animal > > > Output: > ----------- > Original:turkey > > in block:cow > in block:camel > > Afterwards:camel Block parameters actually use assignment semantics, so if a variable already exists, it gets assigned to. The animal in the pipes is the same as the animal before the block. If you use a new local variable, it will come into being just for the block. Before a hundred-message thread gets spawned, let me add that yes, it's going to change in 2.0 :-) I'm actually one of the few people who like it the way it is, but Matz has said that he considers it a design mistake and wants to change it. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black