I’ve been able to wrap individual methods in other calls via alias_method for a while now, but I’ve always done it by hand; been meaning to write something up to do it generically… Well, I came across an ugly but effective bit of code from Tadayoshi Funaba in Programming Ruby that was easily adapted to do just what I need.
Outputs:
$ ruby test.rb I, [2008-09-10T09:44:07.371000 #3292] INFO -- : [1, 2] 1/2
…Thanks to that “before :bar, :log”, log() automatically got called before bar() with the same arguments.
Here’s the alteration to Module that does it:
Module#after() should work similarly.
I experimented with a declaration like this, but without as much success:
…it complains whatever variables or methods referenced in the block aren’t defined, which I guess they aren’t at the time the block is evaluated. Well, I’m sure there’s a workaround for that too. Meanwhile, this is pretty similar to before_filter, which is good enough for Rails.