modulization

This commit is contained in:
Loic Nageleisen 2012-11-06 16:47:46 +01:00
parent 6bd9fef9af
commit 755bd3bc11

16
tee.rb
View file

@ -8,6 +8,7 @@ module Enumerable
end
class IO
module Chunkable
def chunks(chunk_size=1024)
Enumerator.new { |y| y << read(chunk_size) until eof? }
end
@ -15,7 +16,9 @@ class IO
def each_chunk(chunk_size=nil)
chunks.each { |*args| yield *args }
end
end
module Digestable
def digest_with(digest, chunk_size=nil)
chunks(chunk_size).each { |chunk| digest << chunk }
digest
@ -24,7 +27,9 @@ class IO
def sha256(chunk_size=nil)
digest_with Digest::SHA2.new(256), chunk_size
end
end
module Utils
def tee(*procs)
ios = procs.map { |proc| FiberChunkedIO.new &proc }
@ -36,6 +41,11 @@ class IO
ios.each { |io| io.close }
ios.map { |io| io.result }
end
end
include Chunkable
include Digestable
include Utils
end
class FiberChunkedIO
@ -83,11 +93,7 @@ class FiberChunkedIO
@chunks.shift
end
def chunks(chunk_size=1024)
Enumerator.new do |y|
y << read(chunk_size) until eof?
end
end
include IO::Chunkable
end
File.open("test") do |f|