https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112019-07-19T20:22:43ZRuby Issue Tracking SystemRuby master - Bug #9083: BasicObject#method_missing does not always raise NoMethodError for missing methodshttps://bugs.ruby-lang.org/issues/9083?journal_id=797412019-07-19T20:22:43Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li><li><strong>Backport</strong> deleted (<del><i>1.9.3: UNKNOWN, 2.0.0: UNKNOWN</i></del>)</li></ul><p><code>NoMethodError</code> is a subclass of <code>NameError</code>. I think what you are running into is:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">BasicObject</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">foo</span> <span class="c1"># NoMethodError</span>
<span class="no">BasicObject</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">instance_exec</span><span class="p">{</span><span class="n">foo</span><span class="p">}</span> <span class="c1"># NameError</span>
</code></pre>
<p>In both of these cases, <code>BasicObject#method_missing</code> is called, but that method has some magic to determine which error to raise. This behavior in both cases is expected, because the <code>foo</code> in the second case would be a local variable reference if there was a local variable reference in scope. Ruby does not know whether your intention was to reference a local variable or call a method, and therefore uses the more generic <code>NameError</code> instead of the more specific <code>NoMethodError</code> in that case. In the first case, the <code>foo</code> is definitely a method call and not a local variable reference, so Ruby can use a more specific error.</p>