Skip to content

Instantly share code, notes, and snippets.

@kizashi1122
Last active April 27, 2019 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kizashi1122/80c1e2df5882d4abc94a74fc410fd446 to your computer and use it in GitHub Desktop.
Save kizashi1122/80c1e2df5882d4abc94a74fc410fd446 to your computer and use it in GitHub Desktop.
Understanding Enumerator class of Ruby.
class Response
def response_body=(enum)
enum.each {|e| puts "res: #{e}" }
end
end
def gen_stream
Enumerator.new do |yielder|
10.times do |num|
puts "stream: #{num}"
yielder << num
end
end
end
Response.new.response_body = gen_stream
## Output
# stream: 0
# res: 0
# stream: 1
# res: 1
# stream: 2
# res: 2
# stream: 3
# res: 3
# stream: 4
# res: 4
# stream: 5
# res: 5
# stream: 6
# res: 6
# stream: 7
# res: 7
# stream: 8
# res: 8
# stream: 9
# res: 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment