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
-CWS
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
-CWS
Claus Spitzer wrote:
How could I know which method (if any) is currently being executed? E.g.
class Foo
def bar
self.inspect
end
endTells 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
Hmm. So there is no built-in way of doing this? What about lambdas?
Should the result be nil, or stay silent?
irb(main):001:0> def my_name_is
irb(main):002:1> caller[0].split( /:in /).last.gsub( /[\'|\`]/, '' )
irb(main):003:1> end
=> nil
irb(main):004:0> 1.upto(3){ |i|
irb(main):005:1* p i
irb(main):006:1> my_name_is
irb(main):007:1> }
1
2
3
=> 1
irb(main):008:0>
even more interesting is
irb(main):011:0> 1.upto(3){ |i|
irb(main):012:1* p i
irb(main):013:1> p my_name_is
irb(main):014:1> }
1
"irb_binding"
2
"irb_binding"
3
"irb_binding"
=> 1
irb(main):015:0>
Cheers!
-CWS
On Mon, 19 Jul 2004 04:28:10 +0900, James Britt <jamesunderbarb@neurogami.com> wrote:
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 )
endsomething 10
fooJames
James Britt wrote:
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
trace { "x + y + z" } # Show this to Java(tm) fans.
Using Binding.of_caller (available from http://noegnud.sourceforge.net/flgr/binding_of_caller.rb\) this can be simplified to trace "x + y + z":
require 'binding_of_caller'
def trace(expr)
Binding.of_caller do |context|
result = eval(expr, context)
puts "trace: #{expr} = #{result.inspect}"
return result
end
end
I'm using it like this:
irb(main):026:0> x = 2; y = 3; trace "(x + y) ** (x * y)"
trace: (x + y) ** (x * y) = 15625
=> 15625
Regards,
Florian Gross