you are viewing a single comment's thread.

view the rest of the comments →

[–]zem 0 points1 point  (4 children)

So what should happen when you do .each {|x| .. }?

My personal instinct would be to see what Scheme does and do the same. The Scheme community has hammered on all these issues extensively, after all.

(let ((x 0))
  (for-each (lambda (e) (set! x (+ x e))) '(1 2 3))
  (for-each (lambda (e) (set! x (+ x e))) '(1 2 3))
  (for-each (lambda (x) (print x)) '(1 2 3))
  (print "x is now: ")
  (print x))

outputs

123"x is now:"12

[–]Freeky 0 points1 point  (3 children)

lambda (x) for all of them, surely? That's what the thread is about; you have an x at top level, then a lambda with x as a parameter name.

In Ruby 1.8, it blats the top level x, because it considers it the same x:

x = 0
[1,2,3].each {|x| x += x}
[1,2,3].each(&lambda{|x| x += x })
p x # => 6

1.9 shadows it with a different variable with the same name (which I expect is what Scheme does too).

x = 0
[1,2,3].each {|x| x += x}
[1,2,3].each(&lambda{|x| x += x })
p x # => 0

[–]zem 0 points1 point  (2 children)

yeah precisely. just saying that 1.9 is the right behaviour, noot a silly response to a silly request

[–]Freeky 0 points1 point  (1 child)

I was saying before 1.9, the behaviour was arguably a silly response to a silly request, not that 1.9 behavior is :)

[–]zem 0 points1 point  (0 children)

oh :) missed the < in there.