Skip to content

Instantly share code, notes, and snippets.

@ochronus
Created November 25, 2011 11:20
Show Gist options
  • Save ochronus/1393303 to your computer and use it in GitHub Desktop.
Save ochronus/1393303 to your computer and use it in GitHub Desktop.
Simple process ring using ruby continuations
require "continuation" # ruby 1.9, 1.8 doesn't need this require
class Ring
def initalize
@next_node = nil
end
attr_accessor :next_node
def create_continuation
passesleft, message = callcc { |cont|
return cont
}
if passesleft <= 0
puts "done"
exit 0
end
@next_node.call(passesleft - 1, message)
end
end
def run(number_of_rings, cycles, message)
processes = Array.new(number_of_rings) { Ring.new }
continuations = processes.collect { |p| p.create_continuation }
processes.each_with_index { |p,i| p.next_node = continuations[(i+1) % number_of_rings] }
continuations[0].call(number_of_rings * cycles, message)
end
run ARGV[0].to_i, ARGV[1].to_i, "The quick brown fox jumps over the lazy dog"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment