Useless Ruby Tricks: DATA and __END__

So maybe not totally useless certainly fun. Normally, ruby scripts are finished when you reach the end of a file; however, this is not always the case. You can end your script sooner by using the __END__ keyword in your script. Once added, everything you type after that will not be parsed by ruby.

So what?

Well, you can use the global variable DATA to get the contents of what you wrote after the __END__ block. DATA is actually a File object to just that piece of text in your script.

How about a little sample to make things a little clearer?

#!/usr/bin/env ruby
%w(yaml pp).each { |dep| require dep }

obj = YAML::load(DATA)

pp obj

__END__
---
-
  name: Adam
  age: 28
  admin: true
-
  name: Maggie
  age: 28
  admin: false

So with this, I was able to embed a little bit of YAML directly into my script. I added the YAML after __END__. YAML::load will accept a File object, so I just passed it DATA and now I have a reconstituted array.

Maybe that’s not the most practical use of this information; however, Sinatra provides a really awesome use of this.  With Sinatra, you can create a whole web application that lives in one single, solitary ruby file.  Sinatra can use in-file templates.  In-file templates are added into the space after __END__ where each view file is annotated with @@view_name.

require 'rubygems'
require 'sinatra'

get '/' do
  haml :index
end

__END__

@@ layout
%html
  = yield

@@ index
%div.title Hello world!!!!!

Not bad for a useless trick!

  • Pingback: malev's blog » Sinatra en un solo archivo

  • Pingback: Clone TinyURL in 40 lines of Ruby code « saush

  • Riccardo

    About DATA… I just discovered that it is just a File associated with the current script opened for reading and “seeked” after __END__. Try this:

    puts DATA.path

    or

    DATA.seek(0, 0)
    puts DATA.read

    and you’ll have your script printed…

    • http://www.marnen.org Marnen Laibow-Koser

      Cool trick! For that, though, you might as well just use File.read(__FILE__), no?

  • http://hubpages.com/hub/Computer-Tricks-You-Need-To-Know computertricks

    … This really is just what I have been browsing for. Exactly how long did it take to write this? Thanks a bucket load for this superb knowledge.

  • Jeff W

    Awesome! I had no idea about these features, but I can already think of uses for them.

  • http://recuperararchivosformateados.com Recuperar Archivos Formateados

    Gracias por los comentarios. Muy buen sitio, suerte!

  • Pingback: Truques de Ruby: DATA e __END__ « Tino Gomes

  • Pingback: Testing for Ruby DATA object |

  • Julien A.

    an important thing to note is that this trick only works for one file application, if you require a file with a __END__ part the DATA constants won’t be defined.
    Too bad otherwise it could be nice for tests.

  • Beekeeper

    How do I update the DATA file at the end?

    Thanks!