Issue #8839 has been updated by mame (Yusuke Endoh).


headius (Charles Nutter) wrote:
> 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

Use:

  meta = self.singleton_class


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

Your way is too easy to overlook ".init" because a method call is not usually expected there.


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

"It makes no sense" is not a good reason to change anything.
Rather, it is consistent and compatible to just return the last value.
We need better reason to break compatibility, I think.

This is my personal opinion, of course.

-- 
Yusuke Endoh <mame / tsg.ne.jp>
----------------------------------------
Feature #8839: Class and module should return the class or module that was opened
https://bugs.ruby-lang.org/issues/8839#change-42060

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/