Project

General

Profile

Actions

Bug #12696

closed

Defining anonymous classes via `Class.new {}` does not run `class_eval` on the block

Added by kklimuk (Kirill Klimuk) over 7 years ago. Updated over 7 years ago.

Status:
Closed
Assignee:
Target version:
-
[ruby-core:77023]

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.

Actions #1

Updated by nobu (Nobuyoshi Nakada) over 7 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

Also available in: Atom PDF

Like0
Like0