# Instance\_eval with method proc

**URL:** <https://www.ruby-forum.com/t/instance-eval-with-method-proc/177573>\
**Category:** Ruby\
**Created:** [October 20, 2009, 1:17pm UTC](https://www.ruby-forum.com/t/instance-eval-with-method-proc/177573 "2009-10-20T13:17:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [October 20, 2009, 1:17pm UTC](https://www.ruby-forum.com/t/instance-eval-with-method-proc/177573/1 "2009-10-20T13:17:54Z")

</div>

Hi

I’m trying to invoke a method defined in one class with self set to an  
instance of a different class. I didn’t have much success (as shown  
below). Is there another way to achieve this?

Many thanks

Paul

* * *

class Foo  
def foo  
print  
end

def print  
puts “foo”  
end

def test bar  
foo\_proc = Proc.new { print }

```
# Prints bar
bar.instance_eval(&foo_proc)

# 1.8.6 Fails with "wrong number of arguments (1 for 0) 

```

(ArgumentError)"  
# 1.9.1 Prints foo  
bar.instance\_eval(&method(:foo))  
end  
end

class Bar  
def print  
puts “bar”  
end  
end

Foo.new.test Bar.new

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [October 20, 2009, 1:50pm UTC](https://www.ruby-forum.com/t/instance-eval-with-method-proc/177573/2 "2009-10-20T13:50:17Z")

</div>

Paul C. wrote:

> I’m trying to invoke a method defined in one class with self set to an  
> instance of a different class.

If the method is in a common superclass, then you can make an  
UnboundMethod and bind it to the new object. e.g.

> > class Foo; def foo; puts inspect; end; end  
> > =\> nil
> 
> > class Bar \< Foo; end  
> > =\> nil
> 
> > b = Bar.new  
> > =\> #Bar:0xb7c04998
> 
> > Foo.instance\_method(:foo).bind(b).call  
> > #Bar:0xb7c04998  
> > =\> nil
> 
> >

But otherwise I don’t think you can detach arbitrary methods from one  
class and apply them to an object of an unrelated class. You’d probably  
be best putting the relevant method(s) in a mixin, and then sharing that  
between the classes, or adding those methods to the singleton class of  
the individual object you’re interested in.

> > module Baz; def baz; puts “baz!”; end; end  
> > =\> nil
> 
> > s = “hello”  
> > =\> “hello”
> 
> > s.extend Baz  
> > =\> “hello”
> 
> > s.baz  
> > baz!  
> > =\> nil
> 
> >

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [October 20, 2009, 2:02pm UTC](https://www.ruby-forum.com/t/instance-eval-with-method-proc/177573/3 "2009-10-20T14:02:30Z")

</div>

Brian C. wrote:

> If the method is in a common superclass, then you can make an  
> UnboundMethod and bind it to the new object. e.g.
> 
> > > class Foo; def foo; puts inspect; end; end  
> > > =\> nil
> > 
> > > class Bar \< Foo; end  
> > > =\> nil
> > 
> > > b = Bar.new  
> > > =\> #Bar:0xb7c04998
> > 
> > > Foo.instance\_method(:foo).bind(b).call  
> > > #Bar:0xb7c04998  
> > > =\> nil
> > 
> > >

… and of course, object b usually has the ‘foo’ method already  
inherited from the parent, in which case b.send(:foo) is sufficient.

But if ‘foo’ has been overridden, it can still be useful to bind this  
way.

> > class Foo; def foo; puts “foo from parent”; end; end  
> > =\> nil
> 
> > class Bar \< Foo; def foo; puts “foo from child”; end; end  
> > =\> nil
> 
> > b = Bar.new  
> > =\> #Bar:0xb7cf1cfc
> 
> > b.send(:foo)  
> > foo from child  
> > =\> nil
> 
> > m = Foo.instance\_method(:foo)  
> > =\> #\<UnboundMethod: Foo#foo\>
> 
> > m.bind(b).call  
> > foo from parent  
> > =\> nil

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [October 20, 2009, 5:54pm UTC](https://www.ruby-forum.com/t/instance-eval-with-method-proc/177573/4 "2009-10-20T17:54:17Z")

</div>

> > I’m trying to invoke a method defined in one class with self set to an  
> > instance of a different class.
> 
> You’d probably be best … or adding those methods to the singleton  
> class of the individual object you’re interested in.

Thanks Brian, I went with adding the methods to the singleton class.  
Paul
