I know that there are several useful hook methods that Ruby provides. However, I couldn\'t seem to find anything like a \'constant_added
\' hook. The reason I would
I once did it in Ruby 1.9 using Kernel.set_trace_func
. You can keep checking for "line"
events. Whenever such event occurs, take the difference of the constants from the previous time using the constants
method. If you detect a difference, then that is the moment a constant was added/removed.
Kernel.set_trace_func ->event, _, _, _, _, _{
case event
when "line"
some_routine
end
}
Ruby 2.0 may have more powerful API, which allows for a more straightforward way to do it, but I am not sure.