5 ms·
Ruby DATA
- edward 14y agoThis is copied from Perl. See http://stackoverflow.com/questions/161872/hidden-features-of-perl#163700 http://stackoverflow.com/questions/161872/hidden-features-of... my @lines = <DATA>; for (@lines) { print if /bad/; } __DATA__ some good data some bad data more good data more good data
- caiusdurling 14y agoAs quite a good few things in ruby are. Having never really learned perl, I keep discovering them anew in ruby first :-)
- nmcfarl 14y agoAnd widely used in the perl world. It's a good idea for a lot of simple programs.
- oinksoft 14y agoAgreed, this is not a "hidden feature."
- draegtun 14y agoAnd in Perl there are also: __FILE__ , __LINE__ , __PACKAGE__ , __SUB__ & __END__ These are called Special Literals - http://perldoc.perl.org/perldata.html#Special-Literals http://perldoc.perl.org/perldata.html#Special-Literals
- elektronaut 14y agoRuby also has __FILE__ and __LINE__.
- draegtun 14y agoYep. And in Perl6 these were changed to: __LINE__ $?LINE __FILE__ $?FILE __PACKAGE__ $?PACKAGE __END__ =begin END __DATA__ =begin DATA ref: http://feather.perl6.nl/syn/S02.html#Double-underscore_forms http://feather.perl6.nl/syn/S02.html#Double-underscore_forms
- nimrody 14y agoWhich had copied this (including the "DATA" name) from good old BASIC: 10 DATA 1,2,3,4,5,6,7,8,9 20 FOR I=1 TO 9 30 READ A(I) 40 NEXT I
- lukeholder 14y agoI 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.
- caiusdurling 14y agoI'd forgotten sinatra let you do this, I remember it being really nice for a small simple app with a couple of templates.
- bemurphy 14y agoMinor 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/base.rb#L1139 https://github.com/sinatra/sinatra/blob/master/lib/sinatra/b... 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 14y agoI 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 https://github.com/skx/templer [2] - https://raw.github.com/skx/templer/master/lib/Templer/Site/New.pm https://raw.github.com/skx/templer/master/lib/Templer/Site/N...
- caiusdurling 14y agoNice!
- tragomaskhalos 14y agoI 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 !