From: Yukihiro Matsumoto Date: 2006-02-16T03:18:21+09:00 Subject: Re: [ ruby-Bugs-3565 ] Method object used as Object#instance_eval block doesn't work (as expected) Hi, In message "Re: [ ruby-Bugs-3565 ] Method object used as Object#instance_eval block doesn't work (as expected)" on Wed, 15 Feb 2006 19:05:03 +0900, H.Yamamoto writes: |Fowarding.... (Ozgur Murat Homurlu) |http://rubyforge.org/tracker/index.php?func=detail&aid=3565&group_id=426&atid=1698 | | Hello, | Removing the argument which contains the receiver solves | only part of the problem. The other part is instance_eval | doesn't set the self to the receiver. What the code tries | to do is running W#foo in the context of a (that is, in | W#foo, self should be equal to a). A method object is tightly bound to its receiver. Even instance_eval cannot rebind the bound receiver of a method object, mostly because methods simply just don't work well with objects of other types, unlike simple Proc objects written in Ruby. For example, string methods like #split expect their receiver to be strings, and no other type. If rebound is allowed they would just crash. | I know there are other | techniques for this purpose but since Method objects can | be converted to Proc objects and instance_eval runs Proc | object given as a block in the context of the receiver | (that is, self set to the receiver of instance_eval for | the Proc), it's expected to be possible. Considering above reason, we have three choices: a) disallow invoking instance_eval with a proc made from a method at all. b) abandon receiver rebinding for a proc made from a method. c) add type checks for the receivers in every methods, no matter who implemented. these checks could be source of bugs, and could slow down the interpreter a lot. I'd rather choose option b. It's safe, fast, and inconsistency is small. As far as I can imagine, not many people try to rebind the receiver of the bound method using instance_eval(). matz.