Execute code at end of Module/Class, like Ruby test/unit

ruby

Solution

`Test::Unit` uses at_exit for this, which runs code immediately before your application quits:

at_exit do
  puts "printed before quit"
end

.. other stuff

I don't think there is any way to run code specifically after a class or module definition is closed.

Problem

Ruby's unit testing framework executes unit tests even though nobody creates unit test object. For example, in MyUnitTest.rb ``` require 'test/unit' class MyUnitTest < Test::Unit::TestCase def test_true assert true end end ``` and when i invoke that script as ``` ruby MyUnitTest.rb ``` test_true method gets executed automatically. How is this done? I am trying to come up with a framework that can do similarly. I dont want "if __ FILE __ == $0" at the end of every module that uses my framework. thanks.

Original source