CSC/ECE 517 Summer 2008/wiki1 5 bk: Difference between revisions
Jump to navigation
Jump to search
Line 5: | Line 5: | ||
*Debugging problematic code. | *Debugging problematic code. | ||
<code> | <code>module Kernel | ||
module Kernel | |||
alias_method :original_system, :system | alias_method :original_system, :system | ||
def system(*args) | def system(*args) | ||
Line 15: | Line 14: | ||
end | end | ||
end | end | ||
system("ls -al") | system("ls -al")</code> | ||
</code> | |||
==Understanding Aspect Oriented Programming== | ==Understanding Aspect Oriented Programming== |
Revision as of 01:24, 3 June 2008
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")