Created
March 23, 2015 05:08
-
-
Save JoshCheek/7ef19f57b8ac891363e4 to your computer and use it in GitHub Desktop.
Example of a language construct I keep finding myself wanting.
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
def yield_while(&block) | |
Enumerator.new do |y| | |
while value = block.call | |
y.yield value | |
end | |
end | |
end | |
require 'stringio' | |
stream = StringIO.new "abc\ndef\nghi\n" | |
yield_while { stream.gets }.each { |line| puts line } | |
# >> abc | |
# >> def | |
# >> ghi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment