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

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 Over 11 years ago