Project

General

Profile

Actions

Bug #21272

open

Class.new doesn't trigger :class TracePoint

Added by st0012 (Stan Lo) 14 days ago. Updated 6 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:121683]

Description

According to the official documentation:

To filter what is traced, you can pass any of the following as events:

:class
Start a class or module definition.

I'd expect :class events to be triggered when new classes are defined via Class.new as well, but currently that's not the case.

Should we either support Class.new, or clarify the behaviour in documentation?

Reproduction

TracePoint.trace(:class) do |tp|
  puts "Class created at line: #{tp.lineno}"
end

class Foo; end # Triggers the tracepoint

Baz = Class.new # Doesn't trigger the tracepoint

# ruby test.rb
# Class created at line: 5

Updated by Eregon (Benoit Daloze) 6 days ago

In my view, Baz = Class.new doesn't really "Start a class or module definition." it creates a class but there is no definition of it, no body.
Though Baz = Class.new { ... } would arguably start a definition/body, but then so would Baz.class_exec { ... } and that seems less reasonable to catch with a :class TracePoint.

I think unless there is a good motivating example to change behavior (which could be incompatible), it's better to just document it better, so I'd suggest opening a PR to document it better.

Actions

Also available in: Atom PDF

Like0
Like0