#inherited is called right after the class Foo statement. I want something that\'ll run only after the end statement that closes the c
Use TracePoint to track when your class sends up an :end event.
This module will let you create a self.finalize callback in any class.
module Finalize
def self.extended(obj)
TracePoint.trace(:end) do |t|
if obj == t.self
obj.finalize
t.disable
end
end
end
end
Now you can extend your class and define self.finalize, which will run as soon as the class definition ends:
class Foo
puts "Top of class"
extend Finalize
def self.finalize
puts "Finalizing #{self}"
end
puts "Bottom of class"
end
puts "Outside class"
# output:
# Top of class
# Bottom of class
# Finalizing Foo
# Outside class