Skip to content

Instantly share code, notes, and snippets.

@ox
Created November 29, 2011 23:41
Show Gist options
  • Save ox/1407183 to your computer and use it in GitHub Desktop.
Save ox/1407183 to your computer and use it in GitHub Desktop.
A better Ping-Pong example using Actors in Rubinius2.0.0dev. Ends only when you press enter or something
require 'actor'
Ping = Struct.new :sender
Pong = Struct.new :sender
Actor.spawn do
Actor.register(:ping, Actor.current)
loop do
Actor.receive do |msg|
msg.when(Ping) do |p|
puts "ping"
p.sender << Pong.new(Actor.current)
end
end
end
end
Actor.spawn do
Actor[:ping] << Ping.new(Actor.current)
loop do
Actor.receive do |msg|
msg.when(Pong) do |p|
puts "pong"
p.sender << Ping.new(Actor.current)
end
end
end
end
gets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment