CSC/ECE 517 Summer 2008/wiki1 5 bk
Understanding Hooks
The hooking mechanism in Ruby allows certain functional events inside a running program to be identified while providing an action that will occur upon detection of the specified event. In this way, hooks serve as a detector of actions within the code. Examples of where such a technique would prove useful include:
- Recording when or how many times a method is called.
- Recording when an object is created.
- Debugging problematic code.
module Kernel
alias_method :original_system, :system
def system(*args)
puts "Your program executed the \"#{args.join(', ')}\" command."
puts "It was executed at: " + `date`
puts "The command output the following: "
original_system(*args)
end
end
system("ls -al")