Today I Learned

hashrocket A Hashrocket project

Ruby's __END__

I recently used Ruby's __END__ keyword in a client project.

__END__ denotes the end of regular source code in a Ruby file; nothing below it will be executed.

It's essentially a one-line block-commenting device, which is how we used it when trying to recreate a successful pattern borrowed from somewhere else in the project. Instead of toggling between files, we copied beneath __END__ the code we wanted to emulate, started building the new method, and cleaned up before committing.

One benefit of this technique is that any code below __END__ is available via the special filehandle DATA:

# foo.rb
puts DATA.map(&:upcase)

__END__
'foo'
'bar'
'baz'
$ ruby foo.rb
'FOO'
'BAR'
'BAZ'

Documentation

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.