[#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:04394] Re: Scope surprise

From: gotoken@... (GOTO Kentaro)
Date: 2000-08-10 09:09:30 UTC
List: ruby-talk #4394
Hi, 

In message "[ruby-talk:04388] Scope surprise"
    on 00/08/09, Aleksi Niemel<aleksi.niemela@cinnober.com> writes:

>The only difference in following two methods is that other uses 'loop' and
>other 'while true' for "eternal" looping. Well, that's what I thought.
>Actually 'loop' construct uses a block, so even while the code looks same,
>different scoping rules apply, so the outcome is different too. Well, this
>is my explanation, I'm eagerly waiting yours.

Yes, it little confuse people occasionally.  However, one has found a
use of this difference in thread programming [ruby-list:23753].
Consider, for example, a server written with thread.  One can write
typically as follows:

  while true
    cli = server.accept
    Thread.start do
      .... # cli is used here
    end
  end

But cli is not thread local.  A well-known solution is localization by
substitution.

  while true
    tmp = server.accept
    Thread.start do
      cli = tmp    # cli is thread-local
      ....
    end
  end

Now, this localization can be done with loop's block: 

  loop do
    cli = server.accept
    Thread.start do
      ....
    end
  end

Because `loop do' introduces a new block, cli is unique for each 
iteration.  

By the way, more intuitive and while/loop-insensitive way is available
in Ruby 1.6; In current Ruby, Thread.start accepts options as thread
local variable:

  while true
    Thread.start(server.accept) do |cli|
      ....
    end
  end


-- gotoken

# ps.  Aleksi, could you tell me the ruby-talk no. of XMP patch again?
#      I'm sorry for I could not find it. 

In This Thread

Prev Next