https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112013-02-13T18:25:10ZRuby Issue Tracking SystemRuby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=362152013-02-13T18:25:10Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Tracker</strong> changed from <i>Feature</i> to <i>Bug</i></li></ul> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=362202013-02-13T18:32:51Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/36220/diff?detail_id=25687">diff</a>)</li><li><strong>ruby -v</strong> set to <i>2.0.0dev</i></li></ul><p>It's a bug.</p> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=362212013-02-13T18:38:52Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li><li><strong>% Done</strong> changed from <i>0</i> to <i>100</i></li></ul><p>This issue was solved with changeset r39224.<br>
john, thank you for reporting this issue.<br>
Your contribution to Ruby is greatly appreciated.<br>
May Ruby be with you.</p>
<hr>
<p>proc.c: skip prepends</p>
<ul>
<li>proc.c (mnew): skip prepending modules and return the method bound<br>
on the given class. <a href="/issues/7836">[ruby-core:52160]</a> [Bug <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Need a way to get Method and UnboundMethod objects to methods overridden by prepended modules (Closed)" href="https://redmine.ruby-lang.org/issues/7836">#7836</a>]</li>
</ul> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=362912013-02-15T00:57:47Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Closed</i> to <i>Open</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul><p>Matz, could you please confirm?</p>
<p>I'm not sure what is the right approach. What should A.instance_method(:hello) return:</p>
<ol>
<li>the method that <code>A.new.hello</code> will execute, or</li>
<li>the method defined in A?</li>
</ol>
<p>I feel that 1) a more accurate description of the behavior in 1.9.3. It reflects my understanding. Otherwise, strictly speaking, <code>String.instance_method(:object_id)</code> would raise a NameError!</p>
<p>I would expect:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">meth</span> <span class="o">=</span> <span class="no">A</span><span class="p">.</span><span class="nf">instance_method</span> <span class="ss">:hello</span>
<span class="n">a</span> <span class="o">=</span> <span class="no">A</span><span class="p">.</span><span class="nf">new</span>
<span class="n">meth</span><span class="p">.</span><span class="nf">bind</span><span class="p">(</span><span class="n">a</span><span class="p">).</span><span class="nf">call</span> <span class="c1"># should be the same effect as a.bar</span>
<span class="n">meth</span><span class="p">.</span><span class="nf">source_location</span> <span class="c1"># thus should be the same as a.method(:hello).source_location</span>
<span class="n">meth</span><span class="p">.</span><span class="nf">owner</span> <span class="c1"># and thus should be the same as a.method(:hello).owner</span>
</code></pre>
<p>This is always true in Ruby 1.9, and this patch changes that.</p>
<p>I agree with John Mair that there should be a way to get the proper instance method of a class, as he suggests. I would add that <code>String.instance_method(:object_id, false)</code> should raise a NameError, as there is no such instance method defined in String.</p> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=362972013-02-15T06:05:24Zbanister (john mair)jrmair@gmail.com
<ul></ul><p>@ marcandre. Another possible approach is to provide <code>UnboundMethod#super</code>. We do something similar in our Method wrapper for Pry: <a href="https://github.com/pry/pry/blob/master/lib/pry/method.rb#L394-L402" class="external">https://github.com/pry/pry/blob/master/lib/pry/method.rb#L394-L402</a></p>
<p>While we're at it, other useful methods for <code>Method/UnboundMethod</code> could be private?, public?, aliases, singleton? :) but that could be asking for too much ;)</p>
<p>John</p> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=372972013-03-05T20:12:58Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li></ul> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=372982013-03-05T20:13:08Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>% Done</strong> changed from <i>100</i> to <i>0</i></li></ul> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=373002013-03-05T20:32:42Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>File</strong> <a href="/attachments/3573">0001-proc.c-include-prepended-method-flag.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/3573/0001-proc.c-include-prepended-method-flag.patch">0001-proc.c-include-prepended-method-flag.patch</a> added</li></ul> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=373112013-03-06T02:38:07Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>banister (john mair) wrote:</p>
<blockquote>
<p>@ marcandre. Another possible approach is to provide <code>UnboundMethod#super</code>.</p>
</blockquote>
<p>Not a bad idea, for Method also, although I'm not sure if it would be useful to many. You might want to make another feature request for this (and provide a decent justification!).</p>
<blockquote>
<p>While we're at it, other useful methods for <code>Method/UnboundMethod</code> could be private?, public?, aliases, singleton? :) but that could be asking for too much ;)</p>
</blockquote>
<p>These might be more problematic:</p>
<ul>
<li>privacy<br>
It is not really an attribute of the method itself. It's an attribute of the class, i.e. does the class provide public access to a method.</li>
</ul>
<pre><code class="ruby syntaxhl" data-language="ruby"> <span class="k">class</span> <span class="nc">F</span>
<span class="k">def</span> <span class="nf">priv</span><span class="p">;</span> <span class="k">end</span>
<span class="kp">alias_method</span> <span class="ss">:pub</span><span class="p">,</span> <span class="ss">:priv</span>
<span class="kp">private</span> <span class="ss">:priv</span>
<span class="k">end</span>
<span class="no">F</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">pub</span> <span class="c1"># => nil</span>
<span class="no">F</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">priv</span> <span class="c1"># => NoMethodError: private method `priv' called</span>
<span class="no">F</span><span class="p">.</span><span class="nf">instance_method</span><span class="p">(</span><span class="ss">:pub</span><span class="p">)</span> <span class="o">==</span> <span class="no">F</span><span class="p">.</span><span class="nf">instance_method</span><span class="p">(</span><span class="ss">:priv</span><span class="p">)</span> <span class="c1"># => true</span>
</code></pre>
<p>So I think that <code>Module#private_method_defined?</code> and <code>Module#private_instance_methods</code> are the ones you want to use.</p>
<ul>
<li>aliases<br>
Not sure who would use this, but you can already easily do this by comparing the unbound methods:</li>
</ul>
<pre><code class="ruby syntaxhl" data-language="ruby"> <span class="no">String</span><span class="p">.</span><span class="nf">instance_methods</span><span class="p">.</span><span class="nf">group_by</span><span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="no">String</span><span class="p">.</span><span class="nf">instance_method</span><span class="p">(</span><span class="n">x</span><span class="p">)}.</span><span class="nf">map</span><span class="p">(</span><span class="o">&</span><span class="ss">:last</span><span class="p">).</span><span class="nf">reject</span><span class="p">(</span><span class="o">&</span><span class="ss">:one?</span><span class="p">)</span>
<span class="c1"># => [[:==, :===], [:[], :slice], [:length, :size], [:succ, :next], [:succ!, :next!], [:to_s, :to_str], [:concat, :<<],</span>
<span class="p">[</span><span class="ss">:intern</span><span class="p">,</span> <span class="ss">:to_sym</span><span class="p">],</span> <span class="p">[</span><span class="ss">:kind_of?</span><span class="p">,</span> <span class="ss">:is_a?</span><span class="p">],</span> <span class="p">[</span><span class="ss">:send</span><span class="p">,</span> <span class="ss">:__send__</span><span class="p">],</span> <span class="p">[</span><span class="ss">:object_id</span><span class="p">,</span> <span class="ss">:__id__</span><span class="p">],</span> <span class="p">[</span><span class="ss">:to_enum</span><span class="p">,</span> <span class="ss">:enum_for</span><span class="p">]]</span>
</code></pre>
<p>Or if you prefer:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"> <span class="k">class</span> <span class="nc">UnboundMethod</span>
<span class="k">def</span> <span class="nf">aliases</span>
<span class="n">owner</span><span class="p">.</span><span class="nf">instance_methods</span><span class="p">.</span><span class="nf">select</span><span class="p">{</span><span class="o">|</span><span class="n">m</span><span class="o">|</span> <span class="n">owner</span><span class="p">.</span><span class="nf">instance_method</span><span class="p">(</span><span class="n">m</span><span class="p">)</span> <span class="o">==</span> <span class="nb">self</span><span class="p">}</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="no">String</span><span class="p">.</span><span class="nf">instance_method</span><span class="p">(</span><span class="ss">:size</span><span class="p">).</span><span class="nf">aliases</span> <span class="c1"># => [:length, :size]</span>
</code></pre>
<ul>
<li>singleton?</li>
</ul>
<p>This is a property of the owner, no? <code>Module#singleton_class?</code> already accepted: <a href="https://bugs.ruby-lang.org/issues/7609" class="external">https://bugs.ruby-lang.org/issues/7609</a></p>
<p>So if you really want, you will be able to roll your own easily:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"> <span class="k">class</span> <span class="nc">UnboundMethod</span>
<span class="k">def</span> <span class="nf">singleton?</span>
<span class="n">owner</span><span class="p">.</span><span class="nf">is_a?</span><span class="p">(</span><span class="no">Class</span><span class="p">)</span> <span class="o">&&</span> <span class="n">owner</span><span class="p">.</span><span class="nf">singleton_class?</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=373122013-03-06T02:39:49Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Category</strong> set to <i>core</i></li><li><strong>Target version</strong> set to <i>2.1.0</i></li></ul><p>nobu: Patch looks good, but I would go further and have <code>String.instance_method(:object_id, false)</code> also raise a NameError, for consistency with <code>String.instance_methods(false).include? :object_id # => false</code>.</p>
<p>Did Matz confirm any of this?</p> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=374042013-03-09T06:48:55Zprijutme4ty (Ilya Vorontsov)prijutme4ty@gmail.com
<ul></ul><p>Also it should be mentioned that there is no way to get Method for super call. So one cannot know, for example, number of argments of method down the ancestry chain. For prepending methods there is a workaround - to save link on prepend_features. Or to maintain full hierarchy of methods, but it looks akward.<br>
E.g. I needed such method when created #coerce(other,meth) prepending either usual #coerce(other) method or already defined #coerce(other,meth). It's hard to know whether prepended module should call super with one or two arguments.</p> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=447572014-01-30T06:16:45Zhsbt (Hiroshi SHIBATA)hsbt@ruby-lang.org
<ul><li><strong>Target version</strong> changed from <i>2.1.0</i> to <i>2.2.0</i></li></ul> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=469732014-05-30T07:41:35Znobu (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/9781">Feature #9781</a>: Feature Proposal: Method#super_method</i> added</li></ul> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=469752014-05-30T07:43:03Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/46975/diff?detail_id=33891">diff</a>)</li></ul><p>Can't you achieve this by <code>Method#super_method</code>?</p> Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended moduleshttps://redmine.ruby-lang.org/issues/7836?journal_id=678992017-11-22T10:20:35Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul>