Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created March 23, 2015 05:08
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 JoshCheek/7ef19f57b8ac891363e4 to your computer and use it in GitHub Desktop.
Save JoshCheek/7ef19f57b8ac891363e4 to your computer and use it in GitHub Desktop.
Example of a language construct I keep finding myself wanting.
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