https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112017-05-09T04:20:51ZRuby Issue Tracking SystemRuby master - Feature #13551: Add a method to alias class methodshttps://redmine.ruby-lang.org/issues/13551?journal_id=647022017-05-09T04:20:51ZJustJosh (Joshua Stowers)
<ul><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li></ul> Ruby master - Feature #13551: Add a method to alias class methodshttps://redmine.ruby-lang.org/issues/13551?journal_id=647032017-05-09T07:09:55Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>What do you mean by 'intuitive'?</p>
<p>How frequent/important is this?</p>
<p>It's easily possible as follows:</p>
<pre><code>class Array
class << self
alias :my_new :new
end
end
Array.my_new # => []
</code></pre> Ruby master - Feature #13551: Add a method to alias class methodshttps://redmine.ruby-lang.org/issues/13551?journal_id=647042017-05-09T07:19:57Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>Do you mean singleton methods?</p>
<p>Something like this would work:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">Kernel</span>
<span class="k">def</span> <span class="nf">alias_singleton_method</span> <span class="n">new_name</span><span class="p">,</span> <span class="n">old_name</span>
<span class="n">singleton_class</span><span class="p">.</span><span class="nf">class_exec</span> <span class="p">{</span> <span class="kp">alias_method</span> <span class="n">new_name</span><span class="p">,</span> <span class="n">old_name</span> <span class="p">}</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<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">bar</span>
<span class="ss">:bar</span>
<span class="k">end</span>
<span class="n">alias_singleton_method</span> <span class="ss">:baz</span><span class="p">,</span> <span class="ss">:bar</span>
<span class="k">end</span>
<span class="no">Foo</span><span class="p">.</span><span class="nf">bar</span> <span class="c1">#=> :bar</span>
<span class="no">Foo</span><span class="p">.</span><span class="nf">baz</span> <span class="c1">#=> :bar</span>
</code></pre> Ruby master - Feature #13551: Add a method to alias class methodshttps://redmine.ruby-lang.org/issues/13551?journal_id=647282017-05-09T16:23:36ZJustJosh (Joshua Stowers)
<ul></ul><p>Thanks Matthew, that result is exactly what I had in mind.</p>
<p>Having to nest the logic within <code>class << self</code> makes it difficult to understand what is going on, and the code less readable.</p> Ruby master - Feature #13551: Add a method to alias class methodshttps://redmine.ruby-lang.org/issues/13551?journal_id=647362017-05-10T07:43:18Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>JustJosh (Joshua Stowers) wrote:</p>
<blockquote>
<p>Thanks Matthew, that result is exactly what I had in mind.</p>
</blockquote>
<p>Given that it's very easy (as shown by Matthew) to create such a method, do you think it's necessary that this be implemented by Ruby itself? You haven't yet answered the question about frequency of use or use cases.</p> Ruby master - Feature #13551: Add a method to alias class methodshttps://redmine.ruby-lang.org/issues/13551?journal_id=650062017-05-21T17:29:50Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><blockquote>
<p>Given that it's very easy (as shown by Matthew) to create such a method, do you think it's necessary that this<br>
be implemented by Ruby itself? You haven't yet answered the question about frequency of use or use cases.</p>
</blockquote>
<p>Well I do not have a statistical dataset myself, but I use it all the time.</p>
<p>The thing is that for me it is very natural to use "alias".</p>
<pre><code>def foo; puts 'hi from foo()'; end
alias bar foo
</code></pre>
<p>I love it. I love aliases. They make me happy. :)</p>
<p>I use aliases mostly because I have a bad memory. And also because<br>
I want to remain flexible. Some time ago I started to adopt a more<br>
"logical" scheme with my classes, when it may make sense. For example,<br>
most of my classes, if necessary, have a "reset" method. Often I also<br>
have a "menu" method, which I consider as the interface that can<br>
parse the commandline (or optionally any other input that is sent<br>
to it).</p>
<p>And so on and so forth.</p>
<p>So in this context, I totally agree with Joshua Stowers.</p>
<p>I think that ruby itself should not care too much if a user<br>
wants to use an alias on the class/module level instance<br>
or within the context of self with regular instance methods<br>
of the class.</p>
<p>So here, I agree with Joshua.</p>
<p>The awkward thing, though, is that I actually dislike the syntax<br>
proposal:</p>
<p>alias_class_method :new_name, :old_name</p>
<p>The reason is, and this may be trivial, is that I really really<br>
hate the ',' character there. The symbols are ok although it's<br>
better to avoid them.</p>
<p>I also understand that alias_method is not the same as alias,<br>
but alias is so cute and short, it is just built to be loved!</p>
<p>Now you may wonder, how do I alias class methods then?</p>
<p>I use a VERY clumsy way. I do not recommend anyone to use it<br>
and I am sure there are better ways but here goes:</p>
<pre><code>self.instance_eval { alias stop_codons stop_codons? }
</code></pre>
<p>I dislike the self.instance_eval part because it is so long<br>
and verbose - but I love the alias part within the { } because<br>
to my eyes, it is neat and cuddly.</p>
<p>I can't say whether I would use alias_class_method - the name<br>
is not soooo bad (though, what if we have a module Foo; end<br>
method? Do we call it alias_module_method then?), but I am<br>
not sure about the syntax.</p>
<p>BUT I also dislike typing the:</p>
<pre><code>self.instance_eval { }
</code></pre>
<p>part altogether, so in this context, I agree with Joshua Stowers.</p>
<p>(I also understand that one should not use too many aliases but<br>
I love aliases. The main method is usually the one I use the<br>
most, and then I may use some aliases, some for backwards<br>
compatbility and sometimes I remove them too at a later point<br>
so it just provides some more flexibility "as you go".)</p>
<p>Anything that would be as neat or almost as short as:</p>
<p>"alias foo bar"</p>
<p>but for class-methods / module-methods would be great!</p> Ruby master - Feature #13551: Add a method to alias class methodshttps://redmine.ruby-lang.org/issues/13551?journal_id=650072017-05-21T17:33:38Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>Martin showed this example:</p>
<blockquote>
<p>class Array<br>
class << self<br>
alias :my_new :new<br>
end</p>
</blockquote>
<p>Ruby allows this flexibility, this is true. This is also great, we<br>
love it.</p>
<p>But when you use this code and want to distribute it, it is more<br>
cumbersome. And not everyone likes to have modifications that<br>
are non-standard ruby in their code.</p>
<p>I understand that the ruby team wants to be conservative and not<br>
proliferate with lots of extra methods that have only a special<br>
case (say active* gems), but to me I also completely understand<br>
Joshua Stowers here - if possible it is MUCH, much better when<br>
it would be in main ruby. So that everyone could use it.</p>
<ul>
<li>I may use it if it is in ruby core/stdlib.</li>
<li>I may use duck patching (sometimes people use the monkey rather<br>
than the duck) for my local code.</li>
<li>But it is VERY unlikely that I would modify a core class of<br>
ruby like this AND distribute my code to other people too. I<br>
just consider it too cumbersome (and refinements, while the<br>
idea is ok, I never really liked the syntax... and it felt<br>
awkward... but this is for another discussion, lots of time<br>
towards ruby 3.x I hope).</li>
</ul> Ruby master - Feature #13551: Add a method to alias class methodshttps://redmine.ruby-lang.org/issues/13551?journal_id=653672017-06-14T03:08:54ZJustJosh (Joshua Stowers)
<ul></ul><p>I think Robert is exactly right.<br>
I've hoped for such a method on several occasions myself, but highly dislike cluttering up the codebase with logic that is difficult to understand.<br>
One of the best things about Ruby is that when written well it can read almost like English. Such a method would help apply that further.</p> Ruby master - Feature #13551: Add a method to alias class methodshttps://redmine.ruby-lang.org/issues/13551?journal_id=668772017-09-25T08:13:22Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Rejected</i></li></ul><p>As Martin-sensei pointed out, use singleton class notation.</p>
<p>class Foo<br>
class <<Foo<br>
def foo; end<br>
alias bar foo<br>
end<br>
end</p>
<p>I don't think it's worth adding a new method to avoid this simple thing.</p>
<p>Matz.</p> Ruby master - Feature #13551: Add a method to alias class methodshttps://redmine.ruby-lang.org/issues/13551?journal_id=668902017-09-25T09:01:20Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>shevegen (Robert A. Heiler) wrote:</p>
<blockquote>
<p>Martin showed this example:</p>
<blockquote>
<p>class Array<br>
class << self<br>
alias :my_new :new<br>
end</p>
</blockquote>
<p>Ruby allows this flexibility, this is true. This is also great, we<br>
love it.</p>
<p>But when you use this code and want to distribute it, it is more<br>
cumbersome. And not everyone likes to have modifications that<br>
are non-standard ruby in their code.</p>
</blockquote>
<p>Sorry, I should have used another example than Array. This issue is about aliasing class methods, and aliasing a method in a builtin class would be a problem whether it's done with a new feature or with the way I showed. So the fact that I used Array is confusing, but orthogonal to the issue at hand.</p>