Created
December 13, 2012 05:47
-
-
Save udzura/4274342 to your computer and use it in GitHub Desktop.
IO#each_segment
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 IO | |
def each_segment(n) | |
if block_given? | |
while segment = read(n) | |
yield segment | |
end | |
else | |
enum_for(:each_segment, n) | |
end | |
end | |
end | |
## | |
# Usage | |
# I want you lazy!!! | |
File.open('/dev/urandom').each_segment(64).lazy.map{|v| [v].pack('m') }.first 16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment