Using Aspect Oriented Programming in Ruby to simplify tracing


(Note: This was originally submitted to Ruby-Talk on Google Groups, but it
failed to get posted. Hence this was submitted to
gmane.comp.lang.ruby.rails using Outlook Express. Hopefully it will have a
better fate.)

Is there a simple way to automatically insert a trace function invocation at
the start or every method of a rails app? I want to use SimpleTrace to an
IO stream or logger.debug to the log file to output each invoked method's
name and the arguments it's called with, at a minimum.

I understand that's an easy thing to do with Aspect. I see that there's a
patch to Ruby 1.8.2 that introduces a Cut that implements AOP for Ruby.

Has anybody used it? And I wonder whether it would do what I want to do.
(I only skimmed it's description.)

Is there some other implementation "out there?"



An alternative idea: Is it possible to put some kind of
":before ShowFilename",
with the ShowFilename method defined in top-level code that
will be inherited by all other code in the app ... with ShowFilename
writing something like _FILENAME_ evaluated in the context of the
lower-level code?

I've seen some neat Ruby metaprogramming examples, but I'm too new to
Ruby to implement my idea without some guidance.

I got a couple of good answers at

As far as I'm concerned, this thread is dead.

Richard Lionheart wrote: