From: Ken Bloom Date: 2008-03-03T01:09:56+09:00 Subject: Re: What should this print? On Sun, 02 Mar 2008 05:04:47 +0000, Bouquet wrote: > Hi, > > I was expecting the code below to print 1, 1, but I get 1, nil. > > old = nil > [1,1].each do |n| > if n != old > x = 1 > old = n > end > p x > end After the first iteration, old is now 1. In the second iteration, n=1, so 1==1 and the code in if statement doesn't execute. Since x wasn't defined outside of the block, it's created a new (or not) in each iteration, and here it wasn't created, so its value when read (p x) is nil. --Ken -- Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/