[#4567] Re: What's the biggest Ruby development? — Aleksi Niemel<aleksi.niemela@...>

Dave said:

18 messages 2000/08/23
[#4568] Q's on Marshal — Robert Feldt <feldt@...> 2000/08/23

[#4580] RubyUnit testcase run for different init params? — Robert Feldt <feldt@...> 2000/08/25

[#4584] Re: RubyUnit testcase run for different init params? — Dave Thomas <Dave@...> 2000/08/25

Robert Feldt <feldt@ce.chalmers.se> writes:

[#4623] Re: RubyUnit testcase run for different init params? — Robert Feldt <feldt@...> 2000/08/28

On Sat, 26 Aug 2000, Dave Thomas wrote:

[#4652] Andy and Dave's European Tour 2000 — Dave Thomas <Dave@...>

24 messages 2000/08/30
[#4653] Re: Andy and Dave's European Tour 2000 — matz@... (Yukihiro Matsumoto) 2000/08/30

Hi,

[#4657] Ruby tutorials for newbie — Kevin Liang <kevin@...> 2000/08/30

Hi,

[ruby-talk:04393] Re: Scope surprise

From: ts <decoux@...>
Date: 2000-08-10 08:56:46 UTC
List: ruby-talk #4393
>>>>> "A" == Aleksi Niemel=E4?= <iso-8859-1> 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



In This Thread

Prev Next