Misc #10513

instance_eval yields the receiver, but is documented to yield no arguments

Added by ctm (Cliff Matthews) over 6 years ago.



instance_eval yields the receiver, but is documented as yielding no arguments.

I searched the bug reports before writing this up and found bug #2476 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:

 *  call-seq:
 *     obj.instance_eval(string [, filename [, lineno]] )   -> obj
 *     obj.instance_eval {| | block }                       -> obj

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.

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>'


instance_eval (561 Bytes) instance_eval small program that demonstrates block param requirement ctm (Cliff Matthews), 11/14/2014 10:28 PM

No data to display

Also available in: Atom PDF