https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112013-02-23T02:10:46ZRuby Issue Tracking SystemRuby master - Feature #7914: Case for local class methodshttps://bugs.ruby-lang.org/issues/7914?journal_id=367972013-02-23T02:10:46Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>This doesn't seem to be supported by any OO concept I've heard about and basically breaks the conceptual inheritance model in OO in my opinion. I wouldn't like to have to debug a code that behaved like this. It would make understanding an existent code base much harder in my opinion. Don't you think so?</p> Ruby master - Feature #7914: Case for local class methodshttps://bugs.ruby-lang.org/issues/7914?journal_id=367982013-02-23T02:14:07Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>What about the code below?</p>
<p>class Y < X<br>
def special_name<br>
self.class.name == 'Y' ? 'unique:Y' : super<br>
end<br>
end</p> Ruby master - Feature #7914: Case for local class methodshttps://bugs.ruby-lang.org/issues/7914?journal_id=367992013-02-23T02:18:44Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Maybe you could ask for some special method/keyword to know if the class is the same as the declared one (instead of some inheriting class):</p>
<p>def special_name<br>
local_class? ? 'unique:Y' : super<br>
end</p>
<p>Or instead of "local_class" you could ask if it has been called as a "super" method:</p>
<p>def special_name<br>
called_by_super? ? super : 'unique:Y'<br>
end</p> Ruby master - Feature #7914: Case for local class methodshttps://bugs.ruby-lang.org/issues/7914?journal_id=368002013-02-23T02:45:10Zko1 (Koichi Sasada)
<ul><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li><li><strong>Target version</strong> changed from <i>2.1.0</i> to <i>2.6</i></li></ul><p>(I don't have any idea about this ticket. but I feel the name `local' recall perl)</p> Ruby master - Feature #7914: Case for local class methodshttps://bugs.ruby-lang.org/issues/7914?journal_id=368122013-02-23T10:35:38Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>It seems trivial and usually avoidable.</p>
<p>I guess it could achieve with Module#using.</p> Ruby master - Feature #7914: Case for local class methodshttps://bugs.ruby-lang.org/issues/7914?journal_id=368272013-02-23T17:17:38Ztrans (Thomas Sawyer)
<ul></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/2385">@rosenfeld (Rodrigo Rosenfeld Rosas)</a> Maybe I approached this backwards. I just wanted to show one possible use case for supporting local vs. non-local class methods. Your in-method conditional solution works for this specific case, true. But how well does it translate to other cases? For instance, it would not work with anonymous classes. called_by_super is an interesting notion, but after some thought it feels like a make shift approach that only address part of the wider issue. The module non-local side of this is important too. And its use case is much more obvious --everywhere the included/ClassMethods hack is used.</p> Ruby master - Feature #7914: Case for local class methodshttps://bugs.ruby-lang.org/issues/7914?journal_id=368342013-02-24T02:23:12Zfunny_falcon (Yura Sokolov)funny.falcon@gmail.com
<ul></ul><p>Thomas Sawyer, you are the language troll, IMHO.</p>
<p>(But, maybe I'm too)</p>
<p>Everyone else, excuse me for not being polite.<br>
23.02.2013 12:17 пользователь "trans (Thomas Sawyer)" <a href="mailto:transfire@gmail.com" class="email">transfire@gmail.com</a><br>
написал:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: Case for local class methods (Open)" href="https://bugs.ruby-lang.org/issues/7914">#7914</a> has been updated by trans (Thomas Sawyer).</p>
<p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/2385">@rosenfeld (Rodrigo Rosenfeld Rosas)</a> Maybe I approached this backwards. I just wanted to show one<br>
possible use case for supporting local vs. non-local class methods. Your<br>
in-method conditional solution works for this specific case, true. But how<br>
well does it translate to other cases? For instance, it would not work with<br>
anonymous classes. called_by_super is an interesting notion, but after some<br>
thought it feels like a make shift approach that only address part of the<br>
wider issue. The module non-local side of this is important too. And its<br>
use case is much more obvious --everywhere the included/ClassMethods hack<br>
is used.</p>
<hr>
<p>Feature <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: Case for local class methods (Open)" href="https://bugs.ruby-lang.org/issues/7914">#7914</a>: Case for local class methods<br>
<a href="https://bugs.ruby-lang.org/issues/7914#change-36827" class="external">https://bugs.ruby-lang.org/issues/7914#change-36827</a></p>
<p>Author: trans (Thomas Sawyer)<br>
Status: Open<br>
Priority: Normal<br>
Assignee: matz (Yukihiro Matsumoto)<br>
Category: core<br>
Target version: next minor</p>
<p>=begin<br>
Here is a use case for local class methods.</p>
<p>Say we wish to give certain classes and all subclasses a special name.</p>
<p>class X<br>
def self.special_name<br>
"special:#{name}"<br>
end<br>
end<br>
class Y < X; end<br>
class Z < Y; end</p>
<p>Z.special_name #=> "special:Z"</p>
<p>But what if Y has a unique special name?</p>
<p>class Y < X<br>
def special_name<br>
'unique:Y'<br>
end<br>
end</p>
<p>Problem that arises:</p>
<pre><code>Z.special_name #=> "unique:Y" # wrong!
</code></pre>
<p>Currently, to solve this would require creating an additional method, e.g.<br>
<code>unique_name</code> and redefine <code>special_name</code> to first look for unique_name<br>
then fallback to default special name if non-found. It works, but adds<br>
additional complexity to API.</p>
<p>Nicer solution would be local class methods.</p>
<pre><code>class Y < X
def special_name
'unique:Y'
end
local :special_name
end
Y.special_name #=> "unique:Y"
Z.special_name #=> "special:Z"
</code></pre>
<p>The idea being that local class methods are skipped in super/lookup chain.</p>
<p>This idea is not without precedence. Module class methods can be thought<br>
of as being local. So this idea has other side of the notion, that modules<br>
could have class methods that are not skipped over in the super/lookup<br>
chain. In that case we would need a term that means opposite of local, so<br>
I'll use <code>nonlocal</code>:</p>
<pre><code>module M
def self.q; "q"; end
nonlocal :q
end
class X
include M
end
X.q #=> "q"
</code></pre>
<p>=end</p>
<p>--<br>
<a href="http://bugs.ruby-lang.org/" class="external">http://bugs.ruby-lang.org/</a></p>
</blockquote> Ruby master - Feature #7914: Case for local class methodshttps://bugs.ruby-lang.org/issues/7914?journal_id=688952017-12-25T18:15:11Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.6</i></del>)</li></ul>