[ next ] [ prev ] [ contents ] Invitation To Ruby

Coroutines

  1: #!/usr/bin/env ruby
  2: 
  3: require 'coroutine'
  4: first = nil
  5: second = nil
  6: 
  7: puts "Creating First"
  8: first = Coroutine.new {
  9:   puts "First: Doing some work"
 10:   first.switch_to(second)
 11:   puts "First: More work"
 12:   first.switch_to(second)
 13:   puts "First: Done"
 14: }
 15: 
 16: puts "Creating Second"
 17: second = Coroutine.new {
 18:   puts "Second: Kickin' back"
 19:   second.switch_to(first)
 20:   puts "Second: Time for Coffee"
 21:   second.switch_to(first)
 22:   puts "Second: Done"
 23: }
 24: 
 25: first.start
 26: puts "End Of Demo"

Output

Creating First
Creating Second
First: Doing some work
Second: Kickin' back
First: More work
Second: Time for Coffee
First: Done
End Of Demo

(see coroutine.rb Source)



[ next ] [ prev ] [ contents ] Copyright 2002 by Jim Weirich.
All rights reserved.