# Scope issue of variable in iterator

**URL:** <https://www.ruby-forum.com/t/scope-issue-of-variable-in-iterator/85398>\
**Category:** Ruby\
**Created:** [March 22, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/scope-issue-of-variable-in-iterator/85398 "2006-03-22T17:41:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohammad\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Mohammad\_K](https://www.ruby-forum.com/u/Mohammad_K)\
**Post date:** [March 22, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/scope-issue-of-variable-in-iterator/85398/1 "2006-03-22T17:41:48Z")

</div>

mkhan@mkhan:~$ irb  
irb(main):001:0\> batman = ‘robin’  
=\> “robin”  
irb(main):002:0\> [‘cat’, ‘dog’, ‘horse’, ‘chicken’].each { |batman|  
puts batman }  
cat  
dog  
horse  
chicken  
=\> [“cat”, “dog”, “horse”, “chicken”]  
irb(main):003:0\> puts “Now you might think this would say ‘robin’, but  
it says: #{batman}”  
Now you might think this would say ‘robin’, but it says: chicken  
=\> nil  
irb(main):004:0\>

–  
Mohammad K. | Software Engineer  
Lextranet | 107 Union Wharf | Boston, MA 02109  
[http://www.lextranet.com](http://www.lextranet.com)  
(617) 227-4469 Extension 218

Smart Tools. Smart Team. Smart Choice.

THE INFORMATION IN THIS MESSAGE IS INTENDED ONLY FOR THE PERSONAL AND  
CONFIDENTIAL USE OF THE DESIGNATED RECIPIENTS NAMED ABOVE AND MAY  
CONTAIN LEGALLY PRIVILEGED INFORMATION. If the reader of this message  
is not the intended recipient or an agent responsible for delivering  
it to the intended recipient, you are hereby notified that you have  
received this document in error, and that any review, dissemination,  
distribution or copying of this message is strictly prohibited. If  
you have received this communication in error, please notify us  
immediately by telephone at 617-227-4469 Ext. 200. Thank you.

---

<div class="post-metadata">

**Author:** ![Mohammad\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Mohammad\_K](https://www.ruby-forum.com/u/Mohammad_K)\
**Post date:** [March 22, 2006, 5:48pm UTC](https://www.ruby-forum.com/t/scope-issue-of-variable-in-iterator/85398/2 "2006-03-22T17:48:06Z")

</div>

On 3/22/06, Mohammad K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb(main):003:0\> puts “Now you might think this would say ‘robin’, but  
> it says: #{batman}”  
> Now you might think this would say ‘robin’, but it says: chicken  
> =\> nil

It’s true that blocks introduce a new scope.  
However, if they use variables that are in scope outside the block,  
they don’t shadow those variables, they use them.

---

<div class="post-metadata">

**Author:** ![Mohammad\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e8cbc/32.png) [@Mohammad\_K](https://www.ruby-forum.com/u/Mohammad_K)\
**Post date:** [March 22, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/scope-issue-of-variable-in-iterator/85398/3 "2006-03-22T19:04:41Z")

</div>

Mark V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > =\> [“cat”, “dog”, “horse”, “chicken”]  
> > irb(main):003:0\> puts “Now you might think this would say ‘robin’,  
> > but it says: #{batman}”  
> > Now you might think this would say ‘robin’, but it says: chicken  
> > =\> nil
> 
> It’s true that blocks introduce a new scope.  
> However, if they use variables that are in scope outside the block,  
> they don’t shadow those variables, they use them.

… like in other programming languages as well.

```
robert

```
