From: James Britt Date: 2004-07-19T04:28:10+09:00 Subject: Re: Current method Claus Spitzer wrote: > How could I know which method (if any) is currently being executed? E.g. > > class Foo > def bar > self.inspect > end > end > > Tells me which class I'm in, but not which method. How would I go > about doing that? Cheers Here is an example based on something I believe Rick Kilmer posted here some time ago, plus some additional neat Ruby magic for dynamic code tracing (also from Rich, I think, but I might be wrong on that). # Eval a code block using the original context of the variables def trace( &block ) expr = block.call puts "trace: #{expr} = #{eval(expr, block)}" end # Get the name of the immediate caller def my_name_is caller[0].split( /:in /).last.gsub( /[\'|\`]/, '' ) end # An example method to demo the previous two helper methods def something( z ) puts "My name is \"#{my_name_is}\"" x = 5 y = 7 trace { "x + y + z" } # Show this to Java(tm) fans. end # Simple method to demo nested calls to my_name_is # Not quite unit testing, I know ... def foo puts "My name is \"#{my_name_is}\"" something( 12 ) end something 10 foo James