# Same variable name used in block and local question

**URL:** <https://www.ruby-forum.com/t/same-variable-name-used-in-block-and-local-question/54278>\
**Category:** Ruby\
**Created:** [February 28, 2006, 2:57pm UTC](https://www.ruby-forum.com/t/same-variable-name-used-in-block-and-local-question/54278 "2006-02-28T14:57:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubyrus](https://www.ruby-forum.com/u/rubyrus)\
**Post date:** [February 28, 2006, 2:57pm UTC](https://www.ruby-forum.com/t/same-variable-name-used-in-block-and-local-question/54278/1 "2006-02-28T14:57:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rubyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubyrus](https://www.ruby-forum.com/u/rubyrus)\
**Post date:** [February 28, 2006, 3:06pm UTC](https://www.ruby-forum.com/t/same-variable-name-used-in-block-and-local-question/54278/2 "2006-02-28T15:06:57Z")

</div>

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??

No, it isn’t because it was defined outside the block. It’s used in  
cases  
like

count = 0  
arr.each { count += 1 }  
puts “count=#{count}”

If you want to keep it local you must not define it in the surrounding  
scope.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![rubyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubyrus](https://www.ruby-forum.com/u/rubyrus)\
**Post date:** [February 28, 2006, 3:06pm UTC](https://www.ruby-forum.com/t/same-variable-name-used-in-block-and-local-question/54278/3 "2006-02-28T15:06:57Z")

</div>

Hi –

On Tue, 28 Feb 2006, rubyrus wrote:

> * * *
> 
> 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 (removed\_email\_address@domain.invalid)  
Ruby Power and Light ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![rubyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubyrus](https://www.ruby-forum.com/u/rubyrus)\
**Post date:** [February 28, 2006, 5:01pm UTC](https://www.ruby-forum.com/t/same-variable-name-used-in-block-and-local-question/54278/4 "2006-02-28T17:01:01Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > 
> 
> I think that’s a different thing, though, from the parameter  
> semantics. As I understand it, in 2.0 things would work like this:

Is there a difference with regard to scope between

(1…10).each {|foo| }

and

(1…10).each { foo = 1 }

? I think in both cases foo is limited to the block:

16:51:56 [~]: ruby -e ‘def f() (1…5).each {|foo|}; foo end; f’  
-e:1:in `f': undefined local variable or method`foo’ for main:Object  
(NameError)  
from -e:1  
16:51:59 [~]: ruby -e ‘def f() (1…5).each {foo=1}; foo end; f’  
-e:1:in `f': undefined local variable or method`foo’ for main:Object  
(NameError)  
from -e:1  
16:52:04 [~]:

Note, I’m not talking about Ruby 2.

> count = 0  
> other = “zero”  
> [1,2,3].each {|other| count += 1 }
> 
> # count is now 3
> 
> # other is still “zero”
> 
> I may be getting it wrong… but I think the scoping of parameter  
> variables and other block variables is going to diverge.

That’s why I stick to the status quo - I lost track of this discussion  
somewhere. But I trust Matz to do it properly. At least all relevant  
arguments seem to have been exchanged. 🙂

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![rubyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubyrus](https://www.ruby-forum.com/u/rubyrus)\
**Post date:** [February 28, 2006, 3:15pm UTC](https://www.ruby-forum.com/t/same-variable-name-used-in-block-and-local-question/54278/5 "2006-02-28T15:15:19Z")

</div>

Hi –

On Tue, 28 Feb 2006, Robert K. wrote:

> count = 0  
> arr.each { count += 1 }  
> puts “count=#{count}”
> 
> If you want to keep it local you must not define it in the surrounding  
> scope.

I think that’s a different thing, though, from the parameter  
semantics. As I understand it, in 2.0 things would work like this:

count = 0  
other = “zero”  
[1,2,3].each {|other| count += 1 }

# count is now 3

# other is still “zero”

I may be getting it wrong… but I think the scoping of parameter  
variables and other block variables is going to diverge.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![rubyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubyrus](https://www.ruby-forum.com/u/rubyrus)\
**Post date:** [February 28, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/same-variable-name-used-in-block-and-local-question/54278/6 "2006-02-28T17:19:38Z")

</div>

Hi –

On Wed, 1 Mar 2006, Robert K. wrote:

> > > > block should be temporary inside the block, right??
> 
> ? I think in both cases foo is limited to the block:

I guess I had the impression that what puzzled the OP was that using a  
variable as a block parameter (as opposed to just using it in the  
block) was trampling the outer variable. Anyway, as you say, all  
perspectives have been accounted for (many times, counting the  
archives 🙂

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)
