How Ruby lets you keep script and data in *one* file

Updated . Posted . Visible to the public.

The __END__ keyword tells Ruby where a file ends – but you don't have to stop there. Any text you add after it is accessible via the DATA object.

The attached example file data.rb looks like this:

puts DATA.read

__END__
DATA is a File object to everything in the current file after "__END__"

Running it with ruby data.rb prints:

DATA is a File object to everything in the current file after "__END__"
Dominik Schöler
Last edit
Attachments
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2012-10-11 18:33)