Issue #8839 has been updated by headius (Charles Nutter).


mame (Yusuke Endoh) wrote:
> headius (Charles Nutter) wrote:
> > One use:
> > 
> > my_class = class Foo
> >   ...
> > end
> >
> > We can get the reference to a class being created immediately without adding "self" at the end.
> 
> I fail to see why it needs to be a local variable.
> Why don't you use Foo instead of my_class?

How about this:

meta = class << self; end

> It is very arguable if the new idiom should be encouraged.
> Personally, I don't like to see such a code.  Rather, I prefer:
> 
>   class Foo
>     def self.init
>       @foo = Foo.new
>     end
>     self.init
>   end
> 
> or even:
> 
>   class Foo
>     @@foo = Foo.new
>   end

I respect your opinion, but I fail to see why your way is better than mine.

> > Ultimately, the biggest reason I think this should happen is because class Foo; def bar; end; end returning :bar in 2.1 makes very little sense.
> 
> IMHO, most of casual users do not care too much for a return value.

Users not caring about return value is not a good reason to return a nonsensical value. Returning the last method name defined in a class body makes no sense. Returning the class that was just defined may not make sense to you, but it makes sense to me and several others who have commented here.
----------------------------------------
Feature #8839: Class and module should return the class or module that was opened
https://bugs.ruby-lang.org/issues/8839#change-42055

Author: headius (Charles Nutter)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: next minor


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.


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