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


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?


> Another:
> 
> class Foo
>   def self.init
>     @foo = Foo.new
>   end
> end.init
> 
> The use cases I can think of are all fairly subtle, but I think they're valid.

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


> 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.
For example, we often see the following type of code:

  def foo
    internal_ary = []
    # ... building internal_ary ...
    internal_ary.each do |elem|
      # ... using elem ...
    end
  end

Obviously, the author of this method has no intent to return internal_ary.
But, few people writes a code that returns nil explicitly to hide the value.

  def foo
    internal_ary = []
    # ...
    internal_ary.each do |elem|
      # ...
    end
    nil
  end

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

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/