Ruby Issue Tracking System: Issueshttps://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112014-11-14T22:29:49ZRuby Issue Tracking System
Redmine Ruby master - Misc #10513 (Open): instance_eval yields the receiver, but is documented to yield n...https://bugs.ruby-lang.org/issues/105132014-11-14T22:29:49Zctm (Cliff Matthews)ctm@devctm.com
<p>instance_eval yields the receiver, but is documented as yielding no arguments.</p>
<p>I searched the bug reports before writing this up and found bug <a class="issue tracker-1 status-8 priority-4 priority-default closed" title="Bug: instance_eval ArgumentError (Third Party's Issue)" href="https://bugs.ruby-lang.org/issues/2476">#2476</a> which was closed with a message that contained "instance_eval yields the receiver in both 1.8 and 1.9. Unfortunately, a Proc object created by lambda raises ArgumentError when extra arguments are yielded in 1.9.". However such behavior is not expected when the calling sequence is:</p>
<pre><code> * call-seq:
* obj.instance_eval(string [, filename [, lineno]] ) -> obj
* obj.instance_eval {| | block } -> obj
</code></pre>
<p>This discrepancy surprised me, but once I realized what was going on I simply used instance_exec instead of instance_eval. All else equal, I would prefer for instance_eval to to not yield the receiver since I can pick up the receiver as self if I want to, but such a change could breaking existing code, so probably documenting the current behavior is better.</p>
<pre><code>bash-3.2$ ruby --version
ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin14.0]
bash-3.2$ ./instance_eval
This is the lambda that *does* work.
arg = 32
self = 32
./instance_eval:6:in `block in <main>': wrong number of arguments (1 for 0) (ArgumentError)
from ./instance_eval:17:in `instance_eval'
from ./instance_eval:17:in `<main>'
</code></pre>