Skip to content

Instantly share code, notes, and snippets.

@cfcosta
Created January 30, 2012 18:50
Show Gist options
  • Save cfcosta/1705928 to your computer and use it in GitHub Desktop.
Save cfcosta/1705928 to your computer and use it in GitHub Desktop.
require 'fiber'
class Job
include Enumerable
def initialize(jobs)
@jobs = jobs
end
def each(&block)
@jobs.each(&block)
end
def next
@fiber ||= Fiber.new do
each { |e| Fiber.yield e }
raise StopIteration
end
@fiber.resume
end
end
require 'minitest/autorun'
describe Job do
it "should be iterable" do
job = Job.new([1,2,3])
assert_equal job.next, 1
assert_equal job.next, 2
assert_equal job.next, 3
-> { job.next }.must_raise StopIteration
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment