Last active
April 27, 2019 00:53
-
-
Save kizashi1122/80c1e2df5882d4abc94a74fc410fd446 to your computer and use it in GitHub Desktop.
Understanding Enumerator class of Ruby.
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 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