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


shugo (Shugo Maeda) wrote:
> I did a quick hack to try this proposal and found that some test failed with it:
> 
>   https://gist.github.com/shugo/6739085
> 
> For example, bootstraptest/test_block.rb uses the last value of a class definition as follows:

The case given is rather contrived; I have never seen code in the wild use last expression return from a class.

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

This is a fair point, I suppose, but I still see more reasons to make the return value consistent than leave it as is and have classes suddenly returning a symbol for the last defined method. Most folks probably don't even know about the return value since if you're just doing "def" as last expression it has always been nil.
----------------------------------------
Feature #8839: Class and module should return the class or module that was opened
https://bugs.ruby-lang.org/issues/8839#change-42103

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/