Created
March 6, 2018 18:32
-
-
Save cseeger/e821b183ff07450161225531592d098b to your computer and use it in GitHub Desktop.
Fun with Ruby's Enumerator
This file contains 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
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