Created
November 30, 2012 06:33
-
-
Save thirtysixthspan/4174129 to your computer and use it in GitHub Desktop.
When you need a File object but only want part of the file accessible, there is FileSlice
This file contains hidden or 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 FileSlice < File | |
def limit(lower,upper) | |
@lower = lower | |
@upper = upper | |
@offset = @lower | |
rewind | |
end | |
def rewind | |
seek(@lower) | |
end | |
def size | |
(@upper ||= super) - (@lower ||= 0) | |
end | |
def read(length = nil) | |
return nil unless @offset < @upper | |
maxread = @upper - @offset | |
data = super(length ? [length,maxread].min : maxread) | |
@offset = pos | |
data | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment