Require to execute

Have you ever wondered, how it works, that by simply putting something like …

require 'test/unit'

class MyTest < Test::Unit::TestCase

    def test_sample




… you can make a unit test in ruby and simply call that file and everything works? Coming from Java I first thought, perhaps also Ruby classes get have some main methods in them to trigger execution if really nothing else is there in the main-scope to execute. But then I found this code in the test/unit.rb:

at_exit do

  unless $! ||




And yes: at_exit {block} is actually something like that. You can register there things that should be run when the main-scope exits. Sweet :)

Do you want to give me feedback about this article? Please send it to