What should this print?

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

···

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

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

With more context:

It's interesting that these two product different output:

old = nil
[1,1].each do |n|
   if n != old
     x = 1
     old = n
   end
   p x
end

old = nil
for n in [1,1]
    if n != old
      x = 1
      old = n
    end
    p x
end

It's not especially interesting. The block passed to each has a local
variable, x, which is nil in the second iteration because it has never been
set. The for loop does not create a new scope, thus the x variable retains
the value it was set to in the previous iteration, i.e. 1.

Amusingly, if you run the two in the same Ruby instance in the opposite
order (the for loop first), you'd see identical output because in that case
x would have been declared in the outer scope and the block would be using
that x by closure.

--Greg

···

On Thu, Apr 10, 2008 at 11:15:04PM +0900, Boson wrote:

With more context:

It's interesting that these two product different output:

old = nil
[1,1].each do |n|
  if n != old
    x = 1
    old = n
  end
  p x
end

old = nil
for n in [1,1]
   if n != old
     x = 1
     old = n
   end
   p x
end

Gregory Seidman wrote:

It's not especially interesting. The block passed to each has a local
variable, x, which is nil in the second iteration because it has never been
set. The for loop does not create a new scope, thus the x variable retains
the value it was set to in the previous iteration, i.e. 1.

Thanks Greg, I'll read up on the creation of scope.

I'd assumed that "for" was just syntactic sugar for "each".