DATA is one of those features one rarely sees in use, but it can be quite
handy at times. I used it in rcov to include the xx markup generation
library while ensuring the rcov executable remained self-contained (the extension is
optional).
I've written a simple FS meant to be used with DATA, in order to structure
it into individually accessible files. I then used it to implement a very simplistic
pure Ruby compiler (in the sense of composing a .rb file out of many, i.e. a
compiler as the very first ones, before the term started to be misused).
A small FS for the DATA section
I could have used minitar to create POSIX tar files in DATA, but I'd have had to implement
random access on top of it, so I just defined a feeble YAML-based format:
#!/usr/bin/env ruby
# ...
# this is the .rb file
1 + 1
__END__
<length of the toc>
<YAML-serialized toc (obvious from the code)>
data for all the files in the DataFS
just one after the other
Creating the FS
The utterly simplistic API for the Writer class is
datafs=DataFS::Writer.newdatafs.add("filename","file contents")datafs.add("whatever.rb","puts 1")datafs.dump(someIO)# dump to someIOputsdatafs.dump# just return the serialized representation
The implementation is trivial; an array of FStat objects (holding name, content length
and position in the DATA stream) for the embedded files is serialized with YAML and
used as the TOC: