# Question about variable scope

**URL:** <https://www.ruby-forum.com/t/question-about-variable-scope/83619>\
**Category:** Ruby\
**Created:** [January 27, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/question-about-variable-scope/83619 "2007-01-27T18:56:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@robin](https://www.ruby-forum.com/u/robin)\
**Post date:** [January 27, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/question-about-variable-scope/83619/1 "2007-01-27T18:56:01Z")

</div>

Does the following code behave as you’d expect?

2.times{|n| i,j = n,i; puts “i=#{i}, j=#{j.inspect}”}

Can you explain why j is nil rather than 0, the second time round?

Thanks,  
Robin

---

<div class="post-metadata">

**Author:** ![robin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@robin](https://www.ruby-forum.com/u/robin)\
**Post date:** [January 27, 2007, 7:36pm UTC](https://www.ruby-forum.com/t/question-about-variable-scope/83619/2 "2007-01-27T19:36:09Z")

</div>

“robin” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote/schrieb  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 2.times{|n| i,j = n,i; puts “i=#{i}, j=#{j.inspect}”}
> 
> Can you explain why j is nil rather than 0, the second time round?

I’ll try. Everytime the block is executed, new variables n, i and j  
are created, because they don’t already exist outside the block. If  
you want i to be shared, it should already exist before, e.g.:

i = nil  
2.times{|n| i,j = n,i; puts “i=#{i}, j=#{j.inspect}”}

Regards  
Thomas

---

<div class="post-metadata">

**Author:** ![robin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@robin](https://www.ruby-forum.com/u/robin)\
**Post date:** [January 27, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/question-about-variable-scope/83619/3 "2007-01-27T20:27:18Z")

</div>

On 1/27/07, Thomas H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> you want i to be shared, it should already exist before, e.g.:
> 
> i = nil  
> 2.times{|n| i,j = n,i; puts “i=#{i}, j=#{j.inspect}”}

maybe OP thought that  
i,j = n,i  
is semantically equivalent to  
i=n  
j=i  
but it is not :(, which is good 🙂  
the assignments are performed in parallel, and i is still nil when  
assigned  
to j.  
But this is very useful thus one can e.g. swap  
the value of two variables as follows

a,b = b,a

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![robin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@robin](https://www.ruby-forum.com/u/robin)\
**Post date:** [January 28, 2007, 1:05am UTC](https://www.ruby-forum.com/t/question-about-variable-scope/83619/4 "2007-01-28T01:05:56Z")

</div>

On Jan 27, 6:24 pm, Thomas H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ll try. Everytime the block is executed, new variables n, i and j  
> are created, because they don’t already exist outside the block. If  
> you want i to be shared, it should already exist before, e.g.:
> 
> i = nil  
> 2.times{|n| i,j = n,i; puts “i=#{i}, j=#{j.inspect}”}

Thanks for the reply. I think I’ve succeeded in adjusting my mental  
model enough that this now makes sense. 🙂

Robin
