Guy's Hook Patch for Ruby 1.8
He said it was a toy, so I figured I would play with it. This patch provides a single colon of syntactic sugar for hooking a method. The hook is called before the method call.
class Array
def shift:hook()
p "Removing #{self.first}"
end
end
arr = [1, 2, 3]
arr.shift
The above prints Removing 1. You can run the original method with super.
Kinda interesting. It kind of sparks the mind to think what could be done if we were allowed to add code to blocks. In the case of a hook, we’re merely adding code to the beginning of a method. Hmm. I dunno.
