Issue #8839 has been updated by Franck Verrot.


Shugo Maeda wrote:
> If class definitions are changed to return the defined class, there'll be no way to get values created in class definitions except using global variables or constants, etc.
> I'm not sure whether there's any use case of the current behavior other than testing purposes, but please consider there might be some users of the current behavior.

AFAICT, only one value (arguably it can be a complex object but I've never seen that kind of trick before) can be returned, so you'd still have to use globals/constants anyway. Am I mistaking here?

On a general level, it is surprising that `class ...; end` and `Class.new` don't return the same thing while `def` and `define_method`, something needs to be done to `class` to mimic this logic (even if I don't understand why we don't return a Method object today, like Rubinius does). Teaching Ruby, I'm saying `class` and `Class.new` are equivalent, but they're not with regards to this.

As a side-note, I'd like to present another use case. I've built a quick gem that loads Ruby code with `rb_eval_string_protect` and make this possible:
~~~
# foo.rb
require 'rb_import'
module Foo
  Bar = import("./my_class.rb")
end

f = import("./my_class.rb")

puts f.new.say_hello == Foo::Bar.new.say_hello # => true
puts f == Foo::Bar # => false

# my_class.rb
Class.new do
  def say_hello
    puts "Hello from my_class.rb"
  end
end
~~~

But in `my_class.rb`, I can't simply use a regular class definition "class Foo", I either got to use one of those

~~~
Class.new {}

class Foo
  ...
  self
end

class Foo
  ...
end
Foo
~~~




----------------------------------------
Feature #8839: Class and module should return the class or module that was opened
https://bugs.ruby-lang.org/issues/8839#change-52838

* Author: Charles Nutter
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
With the change for https://bugs.ruby-lang.org/issues/3753, "def" forms now return the symbolic name of the method defined. Because class and module bodies just return the last expression in their bodies, this means they will now usually end up returning a symbol for the last method defined. This does not seem useful or correct.

I think class and module should return a reference to the class or module just opened. This would make the return value useful and consistent.



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