A
hook is a technique that lets you trap some Ruby event, such
as object creation.
The simplest hook technique in Ruby is to intercept calls to methods
in system classes. Perhaps you want to log all the operating system
commands your program executes. Simply rename the method
Kernel::system
[This Eiffel-inspired
idiom of renaming a
feature and redefining a new one is very useful, but be aware that
it can cause problems. If a subclass does the same thing, and
renames the methods using the same names, you'll end up with an
infinite loop. You can avoid this by aliasing your methods to a
unique symbol name or by using a consistent naming convention.]
and substitute it with one of your own that both logs the command and
calls the original
Kernel
method.
module Kernel
alias_method :old_system, :system
def system(*args)
result = old_system(*args)
puts "system(#{args.join(', ')}) returned #{result}"
result
end
end
system("date")
system("kangaroo", "-hop 10", "skippy")
|
produces:
Sun Jun 9 00:09:44 CDT 2002
system(date) returned true
system(kangaroo, -hop 10, skippy) returned false
|
A more powerful hook is catching objects as they are created.
If you can be
present when every object is born, you can do all sorts of interesting
things: you can wrap them, add methods to them, remove methods from them, add them
to containers to implement persistence, you name it. We'll show a
simple example here: we'll add a timestamp to every object as it's
created.
One way to hook object creation is to do our method renaming trick on
Class#new
, the method that's called to allocate space for a new
object. The technique isn't perfect---some built-in objects, such as
literal strings, are constructed without calling
new
---but
it'll work just fine for objects we write.
class Class
alias_method :old_new, :new
def new(*args)
result = old_new(*args)
result.timestamp = Time.now
return result
end
end
|
We'll also need to add a timestamp attribute to every object in the
system. We can do this by hacking class
Object
itself.
class Object
def timestamp
return @timestamp
end
def timestamp=(aTime)
@timestamp = aTime
end
end
|
Finally, we can run a test. We'll create a couple of objects a few
seconds apart and check their timestamps.
class Test
|
end
|
|
obj1 = Test.new
|
sleep 2
|
obj2 = Test.new
|
|
obj1.timestamp
|
� |
Sun Jun 09 00:09:45 CDT 2002
|
obj2.timestamp
|
� |
Sun Jun 09 00:09:47 CDT 2002
|
All this method renaming is fine, and it really does work. However,
there are other, more refined ways to get inside a running
program. Ruby provides several callback methods that let you trap
certain events in a controlled way.