https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112021-08-09T19:00:02ZRuby Issue Tracking SystemRuby master - Feature #18069: `instance_exec` is just ignored when the block is originally a methodhttps://bugs.ruby-lang.org/issues/18069?journal_id=932002021-08-09T19:00:02Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li><li><strong>Backport</strong> deleted (<del><i>2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN</i></del>)</li></ul><p>I don't think the current behavior is a bug. <code>Method#to_proc</code> is currently equivalent to:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Method</span>
<span class="k">def</span> <span class="nf">to_proc</span>
<span class="nb">method</span> <span class="o">=</span> <span class="nb">self</span>
<span class="o">-></span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span> <span class="o">&</span><span class="n">block</span><span class="p">)</span> <span class="k">do</span>
<span class="nb">method</span><span class="p">.</span><span class="nf">call</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">,</span> <span class="o">&</span><span class="n">block</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>You wouldn't expect an <code>instance_exec</code> on that lambda to change the behavior of <code>Method#call</code>. So I think the current behavior is expected.</p>
<p>Note that it's not hard to change the behavior to raise an error in this case (and other cases like <code>module_exec</code>). However, changing the behavior would result in significant backwards compatibility issues. I tried a commit that raises ArgumentError in such a case: <a href="https://github.com/jeremyevans/ruby/commit/3e2db2f01281f2335c638142223f8b24531826bd" class="external">https://github.com/jeremyevans/ruby/commit/3e2db2f01281f2335c638142223f8b24531826bd</a>. However, it broke quite a few tests: <a href="https://github.com/jeremyevans/ruby/runs/3283493124" class="external">https://github.com/jeremyevans/ruby/runs/3283493124</a>. Some of the breakage may be due to implementation choice, but I checked and at least some of the breakage is unavoidable as the tests expect to pass procs created by <code>Method#to_proc</code> to <code>instance_exec</code> (e.g. <code>test_instance_exec_define_method_kwsplat</code>).</p>
<p>As I don't think this is a bug, I'm switching this to a feature request.</p> Ruby master - Feature #18069: `instance_exec` is just ignored when the block is originally a methodhttps://bugs.ruby-lang.org/issues/18069?journal_id=932112021-08-10T05:33:20Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>A method is bound to the internal states of the receiver.<br>
I don't think that removing the receiver from a method makes sense.</p>