Skip to content

Instantly share code, notes, and snippets.

@cseeger
Created March 6, 2018 18:32
Show Gist options
  • Save cseeger/e821b183ff07450161225531592d098b to your computer and use it in GitHub Desktop.
Save cseeger/e821b183ff07450161225531592d098b to your computer and use it in GitHub Desktop.
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