A look at Enumerators and Laziness | AppCasts
https://appcasts.io/blog/a-look-at-enumerators-and-laziness
def data_entries(query_options={}) query_options[:page] = 1 results = {} Enumerator.new do |yielder| loop do raise StopIteration if results[:last_page] == true results = call_api(query_options) results[:data_entries].each { |entry| yielder.yield entry } query_options[:page] += 1 end end end