Skip to content

Instantly share code, notes, and snippets.

@mikewadhera
Created November 3, 2011 04:57
Show Gist options
  • Save mikewadhera/1335804 to your computer and use it in GitHub Desktop.
Save mikewadhera/1335804 to your computer and use it in GitHub Desktop.
class Emitter
def initialize(&block)
@block = block
end
def run(nodes, &block)
nodes.each do |id, node|
block.call(Fiber.new {
@block.call(id, node, self)
}.resume)
end
end
def emit(*args)
Fiber.yield(*args)
end
end
# ruby-1.9.2-p180 :145 > e = Emitter.new { |a,b,c| c.emit(a+b) }
# => #<Emitter:0x000001009485d0 @block=#<Proc:0x00000100948580@(irb):145>>
# ruby-1.9.2-p180 :146 > e.run({1=>2,3=>4}) do |t|
# ruby-1.9.2-p180 :147 > puts "Total: #{t}"
# ruby-1.9.2-p180 :148?> end
# Total: 3
# Total: 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment