Use “DATA” to combine data and code in a single ruby file
Jan 14, 2013
DATA
is in fact an IO object, that you can read from (or anything else you’d do with an IO object). It contains all the content after__END__
in that ruby script file*. (It only exists when the file contains__END__
, and for the first file ruby invokes though. Seefootnotefor more details.)How can we use this, and why indeed do I love this fickle constant? I mostly use it for quick scripts where I need to process text data, rather than piping to STDIN.
DATA.each_line.map(&:chomp).each do |url| `open "#{url}"` end __END__ http://google.com/ http://yahoo.com/
Link: Why I love DATA – Caius Theory via caiustheory.com