Sha256: c7758ace460733cd5b196f1399fa309237c5d86d20929c482e2a56c4234abd5c

Contents?: true

Size: 725 Bytes

Versions: 7

Compression:

Stored size: 725 Bytes

Contents

# Inspired by actors example at http://www.artima.com/weblogs/viewpost.jsp?thread=328540

class Bunny {
  def initialize: @id {
    self @@ hop
  }
  def hop {
    self ++ " " print
    self @@ hop
    Thread sleep: 0.5
  }
  def stop: respond_to {
    "Stopping #{self}" println
    die!
    respond_to @@ stopped
  }
  def to_s {
    "Bunny(#{@id})"
  }
}

@thread  = Thread current # save current Thread so it can be resumed
def self stopped {
  @amount = @amount - 1
  { @thread run } if: (@amount == 0)
}

@amount = 10
bunnies = (0..@amount) map: |i| { Bunny new: i }
Console readln: "\n\nPress ENTER to stop bunnies\n\n"
bunnies each: |b| { b @@ stop: self }
Thread stop # wait to be resumed when all bunnies are killed

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
fancy-0.10.0 examples/actor_bunnies.fy
fancy-0.9.0 examples/actor_bunnies.fy
fancy-0.8.0 examples/actor_bunnies.fy
fancy-0.7.0 examples/actor_bunnies.fy
fancy-0.6.0 examples/actor_bunnies.fy
fancy-0.5.0 examples/actor_bunnies.fy
fancy-0.4.0 examples/actor_bunnies.fy