From: ts Date: 2000-08-10T10:56:46+02:00 Subject: [ruby-talk:04393] Re: Scope surprise >>>>> "A" == =?iso-8859-1?Q?Aleksi Niemel=E4?= writes: A> Actually 'loop' construct uses a block, so even while the code looks same, A> different scoping rules apply, so the outcome is different too. Well, this A> is my explanation, I'm eagerly waiting yours. I think this is the right response : A> def works A> var = "init" A> while true `while' is a keyword and it will not create a new scope (this is perhaps not the right word), this mean that "var" *and* "str" are local variables. A> case var A> when "init" A> str = "foo" A> var = "print" A> when "print" A> puts str A> break A> end A> end A> end A> def does_not_work A> var = "init" A> loop do `loop' is not a keyword and in this case ruby create a NODE_ITER, like when you write : [1, 2].each do end this mean that : * "var" is a local variable, because it was found before the iteration * "str" is a *dynamic* variable, it was found first in the iteration each time ruby enter in the body of NODE_ITER it will give new *dynamic* variables (there is an exception), this is why you see a `nil' If you want to have the same output, you need to initialize "str" before the "loop" The exception is, I think, for a `for' : a `for' don't create new *dynamic* variables for each iteration. (`for' is implemented via a NODE_ITER). A> case var Guy Decoux