Created
January 30, 2012 18:50
-
-
Save cfcosta/1705928 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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