
Ruby DATA - EzGraphs
http://caiustheory.com/why-i-love-data
======
edward
This is copied from Perl. See
[http://stackoverflow.com/questions/161872/hidden-features-
of...](http://stackoverflow.com/questions/161872/hidden-features-of-
perl#163700)

    
    
      my @lines = <DATA>;
      for (@lines) {
          print if /bad/;
      }
      
      __DATA__
      some good data
      some bad data
      more good data 
      more good data

~~~
nmcfarl
And widely used in the perl world. It's a good idea for a lot of simple
programs.

~~~
oinksoft
Agreed, this is not a "hidden feature."

------
lukeholder
I learnt about this a while back when sinatra was released, you could have
your html template in the same file. great idea for a micro framework.

~~~
bemurphy
Minor correction (mostly because, I think the code is kinda interesting),
Sinatra does something close to this. It actually uses the __END__ tag for its
inline templates, but then internally it splits the file itself in two and
then parses the latter portion:

[https://github.com/sinatra/sinatra/blob/master/lib/sinatra/b...](https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L1139)

I suspect the reason it does so is for the point the author of the article
makes briefly: "And it only exists for the first ruby file to be invoked by
the interpreter."

------
stevekemp
I wrote a static-site generator, and I wanted to be able to allow users to
deploy new instances easily.

To do that I wrote some code that parses a __DATA__ block for lines like:

mkdir foo create_file foo/bar.txt <<EOF .. This is the file content .. EOF
mkdir bar ..

The process was very simple and allowed me to bundle up the "master template"
files in one simple to understand resource.

[1] - <https://github.com/skx/templer> [2] -
[https://raw.github.com/skx/templer/master/lib/Templer/Site/N...](https://raw.github.com/skx/templer/master/lib/Templer/Site/New.pm)

~~~
caiusdurling
Nice!

------
tragomaskhalos
I had thought that the ability to do a simple quine by rewinding DATA was some
weird implementation bug, so nice to see the rationale here !

