Skip to content

Instantly share code, notes, and snippets.

@thirtysixthspan
Created November 30, 2012 06:33
Show Gist options
  • Save thirtysixthspan/4174129 to your computer and use it in GitHub Desktop.
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
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