Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created September 9, 2011 18:43
Show Gist options
  • Select an option

  • Save practicingruby/1206994 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/1206994 to your computer and use it in GitHub Desktop.
class MyEnumerator
include Enumerable
def initialize(target, *iterator)
@target = target
@iterator = iterator
end
def each(&block)
@target.send(*@iterator, &block)
end
def next
@fiber ||= Fiber.new do
each { |e| Fiber.yield(e) }
raise "End of collection"
end
@fiber.resume
end
def rewind
@fiber = nil
end
end
module Cue
def self.each_word(text)
if block_given?
text.split(/ /).each { |w| yield(w) }
else
MyEnumerator.new(self, :each_word, text)
end
end
end
enum = Cue.each_word("this is a sentence")
p enum.next
p enum.next
p enum.next
p enum.next
p enum.next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment