RubyFlow : The Ruby Community Blog

Home   Submit   Sign Up   Log In   leaders   Twitter   RSS Feed  
 

data-writer - A gem to write to DATA

As most of you probably know in Ruby you can tell the interpreter the source file with the line: __END__

Anything you put below that line will be available as a file object in the DATA constant.

This can be very useful but by can only read from DATA and not write to it. But with data-writer you can!

For example you can do this:

require 'data-writer'
DATAWriter.file("w+") do |w|
w.write("this will be put in the source file after __END__")
end
__END__

So you could for example persist simple information about a script in the source file itself without using a secondary storage file.

To install do gem install data-writer and for documentation check out the github page at https://github.com/kl/data-writer

Comments

You may want to take a look at https://github.com/apeiros/scriptfile - it lets you treat any .rb file like a normal file, reading/writing from/to the data after __END__.
I probably should release it as a gem :)apeiros - July 16, 2012 01:24
Exactly the solution to my current problem, thanks!RyanScottLewis - November 16, 2012 15:46

Post a Comment

Comment abilities for non registered users are currently deactivated, pending time to add a proper CAPTCHA to solve the escalating spam problem. Sorry!