# Q about instance\_eval

**URL:** <https://www.ruby-forum.com/t/q-about-instance-eval/62504>\
**Category:** Ruby\
**Created:** [June 5, 2006, 5:55pm UTC](https://www.ruby-forum.com/t/q-about-instance-eval/62504 "2006-06-05T17:55:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![victor123789](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f0a364/32.png) [@victor123789](https://www.ruby-forum.com/u/victor123789)\
**Post date:** [June 5, 2006, 5:55pm UTC](https://www.ruby-forum.com/t/q-about-instance-eval/62504/1 "2006-06-05T17:55:55Z")

</div>

Hi all.

I tried some strange kind of things.

class DomNode  
attr\_accessor :handler

def event\_handler=(str)  
@handler = eval(str)  
end

def on\_event(\*arg); @handler[\*arg] end  
end

#usage  
node.event\_handler = ‘self[“some-attr”] = “some-value”’  
node.send\_some\_event  
assert\_equal “some-value”, self[“some-attr”]

OK. Then.

class DomNode  
def clone  
other = DomNode.new  
#some initializing, copying of attributes, etc.  
other.handler = @handler #HERE is the problem!  
end  
end

#show the problem:  
node.event\_handler = ‘self[“some-attr”] = “some-value”’  
cloned = node.clone

cloned.send\_some\_event  
assert\_equal “some-value”, self[“some-attr”] #failed!

Event handler has been evaluated in context of original node; so “self”  
is  
pointing to original node, not to cloned. So, I need method to “rebind”  
proc  
to clone, but only if it “binded” to self. How it can be done?

For some reason, I can’t do  
def on\_event(\*arg)  
self.instance\_eval(&@handler)  
end

What can be done instead this?

Thanks.

V.
