Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Fun with Ruby's Enumerator
class Whatever
def run
fetch_paginated_data('Whatever...').each do |item|
puts item
end
end
def fetch_paginated_data(obj)
Enumerator.new do |yielder|
loop do
result = get_data
if result != 0
yielder << result
else
raise StopIteration
end
end
end
end
def get_data
DateTime.now.second % 7
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment