class ENDData
class << self
def for_file(fn = $0)
return DATA if fn == $0
return @cache[fn] if (@cache ||= {})[fn]
str = File.read(fn).scan(/__END__\n(.*?)$/m).last[0] rescue nil
raise(IOError, "#{fn} doesn't have any DATA") unless str
@cache[fn] = StringIO.new(str)
end
def clear_cache(fn)
(@cache ||= {})[fn] = nil
end
end
end
Why a StringIO object instead of a File object? (Cf. last night's discussion on #ruby-lang :-)
David Black
Most operating systems have a limit on the number of filehandles that can be open at any given time, or can restrict it on a per-user basis. If these filehandles are consumed by extensive use of unclosed DATA sections, that limit could be reached quickly. The likelihood of this is increased because I don't think that most people will be used to doing DATA.close.
I'm not opposed to EndData returning filehandles, but I don't think that most of the data will be large enough to warrant the use of a filehandle for each one, because much of the data would be already loaded.
I think a StringIO solution is the simplest one, but an alternate solution would be delegate to a File that opens the file the first time it is used.
-- Paul Brannan
I second that. DATA sections could be really large and thus they should not be kept in memory. IMHO when accessing the data it could be a normal filehandle that overrides the seek operations in order to prevent setting the current position to something before the section tag.
-- Robert Klemme
The data is in memory to some degree already, as Ruby has to have the file in memory at least temporarily.
-- Austin Ziegler
Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog