Issue #12696 has been reported by Kirill Klimuk.

----------------------------------------
Bug #12696: Defining anonymous classes via `Class.new {}` does not run `class_eval` on the block
https://bugs.ruby-lang.org/issues/12696

* Author: Kirill Klimuk
* Status: Open
* Priority: Normal
* Assignee: Zachary Scott
* ruby -v: current
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
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.

~~~ ruby
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.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>