RubyFlow The Ruby and Rails community linklog

×

The Ruby and Rails community linklog

Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!

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 :)

Exactly the solution to my current problem, thanks!

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in