https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112013-08-31T03:34:37ZRuby Issue Tracking SystemRuby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=414392013-08-31T03:34:37Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>When thinking of potential incompatibilities, the only case I could think of where I'd ever used the result of <code>class</code> or <code>module</code> was:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="o"><<</span> <span class="n">foo</span>
<span class="nb">self</span>
<span class="k">end</span>
</code></pre>
<p>Ironically, the proposed change would not introduce an incompatibility in this case :-)</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=414872013-08-31T18:26:03ZAnonymous
<ul></ul><p>+1</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=419772013-09-26T06:41:58Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>So...if there's no objections, can we get this into 2.1? I think with the new def return value change it really needs to happen.</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=419782013-09-26T06:43:11Zenebo (Thomas Enebo)tom.enebo@gmail.com
<ul></ul><p>+1</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=419832013-09-26T11:57:33Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>No, class and def are evaluated in different timings.<br>
This proposal makes no sense.</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=419842013-09-26T12:03:09ZAnonymous
<ul></ul><p>nobu: I don't understand what you mean by "evaluated in different timings". Could you please explain?</p>
<p>I think this proposal is a good idea. For example, this makes no sense:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">A</span>
<span class="k">def</span> <span class="nf">foo</span>
<span class="k">end</span>
<span class="k">end</span> <span class="c1"># => :foo</span>
</code></pre>
<p>But this makes more sense:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">A</span>
<span class="k">def</span> <span class="nf">foo</span>
<span class="k">end</span>
<span class="k">end</span> <span class="c1"># => A</span>
</code></pre> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=419962013-09-26T20:21:46Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li><li><strong>Target version</strong> set to <i>2.6</i></li></ul><p>How useful is this proposal?<br>
I think we should not change anything without consideration of use case.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a></p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=420192013-09-27T04:22:11Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>I've been wondering the same thing since I saw this ticket being created...</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=420202013-09-27T04:22:58Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Actually, I don't understand even why returning a symbol from method definition is useful...</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=420292013-09-27T19:29:25Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>mame (Yusuke Endoh) wrote:</p>
<blockquote>
<p>How useful is this proposal?<br>
I think we should not change anything without consideration of use case.</p>
</blockquote>
<p>One use:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">my_class</span> <span class="o">=</span> <span class="k">class</span> <span class="nc">Foo</span>
<span class="o">...</span>
<span class="k">end</span>
</code></pre>
<p>We can get the reference to a class being created immediately without adding "<code>self</code>" at the end. It also brings some equivalence with <code>my_class = Class.new</code>.</p>
<p>Another:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Foo</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">init</span>
<span class="vi">@foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="p">.</span><span class="nf">new</span>
<span class="k">end</span>
<span class="k">end</span><span class="p">.</span><span class="nf">init</span>
</code></pre>
<p>The use cases I can think of are all fairly subtle, but I think they're valid.</p>
<p>Ultimately, the biggest reason I think this should happen is because <code>class Foo; def bar; end; end</code> returning <code>:bar</code> in 2.1 makes very little sense.</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=420482013-09-27T21:11:01Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>I see now. I really tried a few times to get something like your second example to work:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">MyProcessor</span>
<span class="o">...</span>
<span class="k">end</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">params</span><span class="p">).</span><span class="nf">process</span>
</code></pre>
<p>I'd usually use such pattern in small scripts since methods are not hoisting like functions declarations in JavaScript and I prefer to write code top-down, so I enclose them in a class.</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=420512013-09-27T23:33:41Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>headius (Charles Nutter) wrote:</p>
<blockquote>
<p>One use:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">my_class</span> <span class="o">=</span> <span class="k">class</span> <span class="nc">Foo</span>
<span class="o">...</span>
<span class="k">end</span>
</code></pre>
<p>We can get the reference to a class being created immediately without adding "<code>self</code>" at the end.</p>
</blockquote>
<p>I fail to see why it needs to be a local variable.<br>
Why don't you use <code>Foo</code> instead of <code>my_class</code>?</p>
<blockquote>
<p>Another:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Foo</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">init</span>
<span class="vi">@foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="p">.</span><span class="nf">new</span>
<span class="k">end</span>
<span class="k">end</span><span class="p">.</span><span class="nf">init</span>
</code></pre>
<p>The use cases I can think of are all fairly subtle, but I think they're valid.</p>
</blockquote>
<p>It is very arguable if the new idiom should be encouraged.<br>
Personally, I don't like to see such a code. Rather, I prefer:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Foo</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">init</span>
<span class="vi">@foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="p">.</span><span class="nf">new</span>
<span class="k">end</span>
<span class="nb">self</span><span class="p">.</span><span class="nf">init</span>
<span class="k">end</span>
</code></pre>
<p>or even:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Foo</span>
<span class="vc">@@foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="p">.</span><span class="nf">new</span>
<span class="k">end</span>
</code></pre>
<blockquote>
<p>Ultimately, the biggest reason I think this should happen is because <code>class Foo; def bar; end; end</code> returning <code>:bar</code> in 2.1 makes very little sense.</p>
</blockquote>
<p>IMHO, most of casual users do not care too much for a return value.<br>
For example, we often see the following type of code:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">foo</span>
<span class="n">internal_ary</span> <span class="o">=</span> <span class="p">[]</span>
<span class="c1"># ... building internal_ary ...</span>
<span class="n">internal_ary</span><span class="p">.</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">elem</span><span class="o">|</span>
<span class="c1"># ... using elem ...</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>Obviously, the author of this method has no intent to return internal_ary.<br>
But, few people writes a code that returns nil explicitly to hide the value.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">foo</span>
<span class="n">internal_ary</span> <span class="o">=</span> <span class="p">[]</span>
<span class="c1"># ...</span>
<span class="n">internal_ary</span><span class="p">.</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">elem</span><span class="o">|</span>
<span class="c1"># ...</span>
<span class="k">end</span>
<span class="kp">nil</span>
<span class="k">end</span>
</code></pre>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a></p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=420552013-09-28T03:39:25Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>mame (Yusuke Endoh) wrote:</p>
<blockquote>
<p>headius (Charles Nutter) wrote:</p>
<blockquote>
<p>One use:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">my_class</span> <span class="o">=</span> <span class="k">class</span> <span class="nc">Foo</span>
<span class="o">...</span>
<span class="k">end</span>
</code></pre>
<p>We can get the reference to a class being created immediately without adding "<code>self</code>" at the end.</p>
</blockquote>
<p>I fail to see why it needs to be a local variable.<br>
Why don't you use <code>Foo</code> instead of <code>my_class</code>?</p>
</blockquote>
<p>How about this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">meta</span> <span class="o">=</span> <span class="k">class</span> <span class="o"><<</span> <span class="nb">self</span><span class="p">;</span> <span class="k">end</span>
</code></pre>
<blockquote>
<p>It is very arguable if the new idiom should be encouraged.<br>
Personally, I don't like to see such a code. Rather, I prefer:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Foo</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">init</span>
<span class="vi">@foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="p">.</span><span class="nf">new</span>
<span class="k">end</span>
<span class="nb">self</span><span class="p">.</span><span class="nf">init</span>
<span class="k">end</span>
</code></pre>
<p>or even:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Foo</span>
<span class="vc">@@foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="p">.</span><span class="nf">new</span>
<span class="k">end</span>
</code></pre>
</blockquote>
<p>I respect your opinion, but I fail to see why your way is better than mine.</p>
<blockquote>
<blockquote>
<p>Ultimately, the biggest reason I think this should happen is because <code>class Foo; def bar; end; end</code> returning <code>:bar</code> in 2.1 makes very little sense.</p>
</blockquote>
<p>IMHO, most of casual users do not care too much for a return value.</p>
</blockquote>
<p>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.</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=420582013-09-28T04:20:47Zrobertgleeson (Robert Gleeson)
<ul></ul><p>I agree that returning the class or module makes sense (to me).<br>
I'd also like to see "<code>def foo</code>" return a (<code>Unbound</code>)<code>Method</code> instead of a <code>Symbol</code>.<br>
it seems like that'd also make more sense (not to derail this conversation). A (<code>Unbound</code>)<code>Method</code> is much more useful.<br>
I saw headius mention you can say:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Foo</span>
<span class="nb">instance_method</span> <span class="k">def</span> <span class="nf">foo</span>
<span class="k">end</span> <span class="c1"># => UnboundMethod.</span>
<span class="k">end</span>
</code></pre>
<p>...but seems like a nice default.</p>
<p>+1 to the proposal.</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=420602013-09-28T08:41:21Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>headius (Charles Nutter) wrote:</p>
<blockquote>
<p>mame (Yusuke Endoh) wrote:</p>
<blockquote>
<p>headius (Charles Nutter) wrote:</p>
<blockquote>
<p>One use:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">my_class</span> <span class="o">=</span> <span class="k">class</span> <span class="nc">Foo</span>
<span class="o">...</span>
<span class="k">end</span>
</code></pre>
<p>We can get the reference to a class being created immediately without adding "self" at the end.</p>
</blockquote>
<p>I fail to see why it needs to be a local variable.<br>
Why don't you use <code>Foo</code> instead of <code>my_class</code>?</p>
</blockquote>
<p>How about this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">meta</span> <span class="o">=</span> <span class="k">class</span> <span class="o"><<</span> <span class="nb">self</span><span class="p">;</span> <span class="k">end</span>
</code></pre>
</blockquote>
<p>Use:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">meta</span> <span class="o">=</span> <span class="nb">self</span><span class="p">.</span><span class="nf">singleton_class</span>
</code></pre>
<blockquote>
<blockquote>
<p>It is very arguable if the new idiom should be encouraged.<br>
Personally, I don't like to see such a code. Rather, I prefer:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Foo</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">init</span>
<span class="vi">@foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="p">.</span><span class="nf">new</span>
<span class="k">end</span>
<span class="nb">self</span><span class="p">.</span><span class="nf">init</span>
<span class="k">end</span>
</code></pre>
<p>or even:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Foo</span>
<span class="vc">@@foo</span> <span class="o">=</span> <span class="no">Foo</span><span class="p">.</span><span class="nf">new</span>
<span class="k">end</span>
</code></pre>
</blockquote>
<p>I respect your opinion, but I fail to see why your way is better than mine.</p>
</blockquote>
<p>Your way is too easy to overlook "<code>.init</code>" because a method call is not usually expected there.</p>
<blockquote>
<blockquote>
<blockquote>
<p>Ultimately, the biggest reason I think this should happen is because <code>class Foo; def bar; end; end</code> returning <code>:bar</code> in 2.1 makes very little sense.</p>
</blockquote>
<p>IMHO, most of casual users do not care too much for a return value.</p>
</blockquote>
<p>Users not caring about return value is not a good reason to return a nonsensical value.</p>
</blockquote>
<p>"It makes no sense" is not a good reason to change anything.<br>
Rather, it is consistent and compatible to just return the last value.<br>
We need better reason to break compatibility, I think.</p>
<p>This is my personal opinion, of course.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a></p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=420682013-09-28T15:39:55Zshugo (Shugo Maeda)
<ul></ul><p>I did a quick hack to try this proposal and found that some test failed with it:</p>
<p><a href="https://gist.github.com/shugo/6739085" class="external">https://gist.github.com/shugo/6739085</a></p>
<p>For example, bootstraptest/test_block.rb uses the last value of a class definition as follows:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">assert_equal</span> <span class="s1">'ok'</span><span class="p">,</span> <span class="sx">%q{
STDERR.reopen(STDOUT)
class C
define_method(:foo) do |&block|
block.call if block
end
result = "ng"
new.foo() {result = "ok"}
result
end
}</span>
</code></pre>
<p>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.<br>
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.</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=421032013-09-30T22:20:05Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>shugo (Shugo Maeda) wrote:</p>
<blockquote>
<p>I did a quick hack to try this proposal and found that some test failed with it:</p>
<p><a href="https://gist.github.com/shugo/6739085" class="external">https://gist.github.com/shugo/6739085</a></p>
<p>For example, bootstraptest/test_block.rb uses the last value of a class definition as follows:</p>
</blockquote>
<p>The case given is rather contrived; I have never seen code in the wild use last expression return from a class.</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>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 "<code>def</code>" as last expression it has always been <code>nil</code>.</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=421102013-09-30T23:53:19Zavdi (Avdi Grimm)avdi@avdi.codes
<ul></ul><p>On Sat, Sep 28, 2013 at 2:39 AM, shugo (Shugo Maeda)<br>
<a href="mailto:redmine@ruby-lang.org" class="email">redmine@ruby-lang.org</a>wrote:</p>
<blockquote>
<p>For example, bootstraptest/test_block.rb uses the last value of a class<br>
definition as follows:</p>
</blockquote>
<p>The only time I've ever used the return value of a class definition it's<br>
been to get the class itself, by making <code>self</code> the last statement in the<br>
class definition.</p>
<p>That said, if you really wanted to preserve the ability to return something<br>
other from a class definition, could you make <code>break <SOME_VALUE></code> override<br>
the return, as it does in blocks?</p>
<p>--<br>
Avdi Grimm<br>
<a href="http://avdi.org" class="external">http://avdi.org</a></p>
<p>I only check email twice a day. to reach me sooner, go to<br>
<a href="http://awayfind.com/avdi" class="external">http://awayfind.com/avdi</a></p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=421192013-10-01T09:37:52Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>avdi (Avdi Grimm) wrote:</p>
<blockquote>
<p>That said, if you really wanted to preserve the ability to return something<br>
other from a class definition, could you make <code>break <SOME_VALUE></code> override<br>
the return, as it does in blocks?</p>
</blockquote>
<p>That wouldn't be backward-compatible with anyone expecting last expression, but it's an excellent idea to address Shugo's concern.</p> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=427952013-11-07T17:34:47Zjballanc (Joshua Ballanco)jballanc@gmail.com
<ul></ul><p>Just to throw my 2¢ in...</p>
<p>I think the main benefit to returning a symbol from <code>def</code> is that it enables the use of method decorators. Similarly, I would be in favor of returning the class defined from <code>class</code> so that we could also build class decorators. A trivial example:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">enumerable</span><span class="p">(</span><span class="n">klass</span><span class="p">)</span>
<span class="n">klass</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="ss">:include</span><span class="p">,</span> <span class="no">Enumerable</span><span class="p">)</span>
<span class="n">klass</span>
<span class="k">end</span>
<span class="n">enumerable</span>
<span class="k">class</span> <span class="nc">Foo</span>
<span class="c1">#...</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=428072013-11-08T04:04:11Zheadius (Charles Nutter)headius@headius.com
<ul></ul><p>jballanc (Joshua Ballanco) wrote:</p>
<blockquote>
<p>Just to throw my 2¢ in...</p>
<p>I think the main benefit to returning a symbol from <code>def</code> is that it enables the use of method decorators. Similarly, I would be in favor of returning the class defined from <code>class</code> so that we could also build class decorators. A trivial example:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">enumerable</span><span class="p">(</span><span class="n">klass</span><span class="p">)</span>
<span class="n">klass</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="ss">:include</span><span class="p">,</span> <span class="no">Enumerable</span><span class="p">)</span>
<span class="n">klass</span>
<span class="k">end</span>
<span class="n">enumerable</span>
<span class="k">class</span> <span class="nc">Foo</span>
<span class="c1">#...</span>
<span class="k">end</span>
</code></pre>
</blockquote>
<p>Clever! Though I don't think that would parse the way you want. This would work though, obviously:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"> <span class="n">enumerable</span> <span class="k">class</span> <span class="nc">Foo</span> <span class="o">...</span>
</code></pre> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=472502014-06-16T21:05:09Zalexeymuranov (Alexey Muranov)
<ul></ul><p>+1. I do not want to share my silly code where i would use it, but currently it may look something like that:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">MyClass</span>
<span class="c1"># ...</span>
<span class="nb">self</span>
<span class="k">end</span><span class="p">.</span><span class="nf">define_more_methods</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">).</span><span class="nf">freeze</span>
</code></pre> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=521952015-04-19T10:46:26Zsawa (Tsuyoshi Sawada)
<ul></ul><p>I think this proposal would be useful in cases like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Foo</span>
<span class="n">prepend</span> <span class="k">module</span> <span class="nn">Bar</span>
<span class="k">def</span> <span class="nf">baz</span>
<span class="o">...</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=528382015-06-11T12:13:34Zcesario (Franck Verrot)franck@verrot.fr
<ul></ul><p>Shugo Maeda wrote:</p>
<blockquote>
<p>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.<br>
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.</p>
</blockquote>
<p>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?</p>
<p>On a general level, it is surprising that <code>class ...; end</code> and <code>Class.new</code> don't return the same thing while <code>def</code> and <code>define_method</code>, something needs to be done to <code>class</code> 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 <code>class</code> and <code>Class.new</code> are equivalent, but they're not with regards to this.</p>
<p>As a side-note, I'd like to present another use case. I've built a quick gem that loads Ruby code with <code>rb_eval_string_protect</code> and make this possible:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="c1"># foo.rb</span>
<span class="nb">require</span> <span class="s1">'rb_import'</span>
<span class="k">module</span> <span class="nn">Foo</span>
<span class="no">Bar</span> <span class="o">=</span> <span class="n">import</span><span class="p">(</span><span class="s2">"./my_class.rb"</span><span class="p">)</span>
<span class="k">end</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">import</span><span class="p">(</span><span class="s2">"./my_class.rb"</span><span class="p">)</span>
<span class="nb">puts</span> <span class="n">f</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">say_hello</span> <span class="o">==</span> <span class="no">Foo</span><span class="o">::</span><span class="no">Bar</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">say_hello</span> <span class="c1"># => true</span>
<span class="nb">puts</span> <span class="n">f</span> <span class="o">==</span> <span class="no">Foo</span><span class="o">::</span><span class="no">Bar</span> <span class="c1"># => false</span>
<span class="c1"># my_class.rb</span>
<span class="no">Class</span><span class="p">.</span><span class="nf">new</span> <span class="k">do</span>
<span class="k">def</span> <span class="nf">say_hello</span>
<span class="nb">puts</span> <span class="s2">"Hello from my_class.rb"</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>But in <code>my_class.rb</code>, I can't simply use a regular class definition "class Foo", I either got to use one of those</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Class</span><span class="p">.</span><span class="nf">new</span> <span class="p">{}</span>
<span class="k">class</span> <span class="nc">Foo</span>
<span class="o">...</span>
<span class="nb">self</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">Foo</span>
<span class="o">...</span>
<span class="k">end</span>
<span class="no">Foo</span>
</code></pre> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=558092015-12-28T08:08:51Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/11905">Feature #11905</a>: Change the 'class' keyword to return a symbol</i> added</li></ul> Ruby master - Feature #8839: Class and module should return the class or module that was openedhttps://bugs.ruby-lang.org/issues/8839?journal_id=558122015-12-28T08:36:05Zyuki24 (Yuki Nishijima)yk.nishijima@gmail.com
<ul></ul><p>I find myself repeating writing something like below (which, of course, doesn't work at this moment):</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">private_constant</span> <span class="k">class</span> <span class="nc">Person</span>
<span class="o">...</span>
<span class="k">end</span>
</code></pre>
<p>Although it would need to return a symbol, it would be great if we could do the above.</p>