Actions
Bug #12696
closedDefining anonymous classes via `Class.new {}` does not run `class_eval` on the block
Description
When defining anonymous classes, the documentation comment (https://github.com/ruby/ruby/blob/b9a82eaa13a5438318d78aee0afb907a1d143a51/object.c#L1754) explains that
If a block is given, it is passed the class object, and the block is evaluated in the context of this class using class_eval.
However, it seems that class_eval
is never run.
If it was, then both of the examples below would have the same printed output, but they do not.
class Class
alias_method :old_class_eval, :class_eval
def class_eval(*args, &block)
puts '1. foo'
old_class_eval *args, &block
puts '3. bar'
end
end
# Example 1
some_class = Class.new do
puts '2. baz'
end
# Example 2
other_class = Class.new
other_class.class_eval { puts '2. baz' }
I'm not sure if the documentation is misleading me or if i should be able to hook into class_eval
but can't.
Updated by nobu (Nobuyoshi Nakada) almost 8 years ago
- Status changed from Open to Closed
Applied in changeset r55994.
object.c: fix {Module,Class}.new rdoc [ci skip]
- object.c (rb_mod_initialize, rb_class_initialize): [DOC] these
methods do not invoke module_eval/class_eval, just eval the
given block under the new module/class but sharing the context
with the surrounding scope like those methods.
[ruby-core:77023] [Bug #12696]
Actions
Like0
Like0