instance method
TracePoint#defined_class
() -> (::Class | ::Module)?
defined_class
trace_point.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/trace_point.rb, line 375
def defined_class
Primitive.tracepoint_attr_defined_class
end
Returns the class or module of the method being called.
Example
class C; def foo; end; end trace = TracePoint.new(:call) do |tp| p tp.defined_class #=> C end.enable do C.new.foo end
If the method is defined by a module, then that module is returned.
Example
module M; def foo; end; end class C; include M; end trace = TracePoint.new(:call) do |tp| p tp.defined_class #=> M end.enable do C.new.foo end
Note: defined_class returns the singleton class.
The 6th block parameter of Kernel#set_trace_func passes the original class attached by the singleton class.
This is a difference between Kernel#set_trace_func and TracePoint.
Example
class C; def self.foo; end; end trace = TracePoint.new(:call) do |tp| p tp.defined_class #=> #<Class:C> end.enable do C.foo end