# Write up on hazards of instance\_eval?

**URL:** <https://www.ruby-forum.com/t/write-up-on-hazards-of-instance-eval/54414>\
**Category:** Ruby\
**Created:** [March 4, 2006, 1:41am UTC](https://www.ruby-forum.com/t/write-up-on-hazards-of-instance-eval/54414 "2006-03-04T01:41:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![itsme213](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/258eb7/32.png) [@itsme213](https://www.ruby-forum.com/u/itsme213)\
**Post date:** [March 4, 2006, 1:41am UTC](https://www.ruby-forum.com/t/write-up-on-hazards-of-instance-eval/54414/1 "2006-03-04T01:41:37Z")

</div>

Is there any good write up on the hazards of instance\_eval? Or do you  
happen  
to have your top-10 on the tip of your fingers?

Thanks.

---

<div class="post-metadata">

**Author:** ![itsme213](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/258eb7/32.png) [@itsme213](https://www.ruby-forum.com/u/itsme213)\
**Post date:** [March 4, 2006, 5:42am UTC](https://www.ruby-forum.com/t/write-up-on-hazards-of-instance-eval/54414/2 "2006-03-04T05:42:54Z")

</div>

On Sat, 4 Mar 2006, itsme213 wrote:

> Is there any good write up on the hazards of instance\_eval? Or do you happen  
> to have your top-10 on the tip of your fingers?
> 
> Thanks.

one thing you have to watch out for it getting alot done in a hurrry -  
if  
that’s a problem for you don’t use it. it’s a bit like swinging a  
machette  
around, but otherwise quite benign.

😉

-a

---

<div class="post-metadata">

**Author:** ![itsme213](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/258eb7/32.png) [@itsme213](https://www.ruby-forum.com/u/itsme213)\
**Post date:** [March 4, 2006, 5:00am UTC](https://www.ruby-forum.com/t/write-up-on-hazards-of-instance-eval/54414/3 "2006-03-04T05:00:19Z")

</div>

Hi –

On Sat, 4 Mar 2006, itsme213 wrote:

> Is there any good write up on the hazards of instance\_eval? Or do you happen  
> to have your top-10 on the tip of your fingers?

I don’t know whether there are ten distinct hazards, but a couple of  
things to keep in mind:

instance\_eval gives you more access to an object’s state than the  
object might have planned on. You can change its instance variables  
and call private methods, for example.

Another thing to be careful about is obscurity, caused by things like  
this:

class C  
def x(&block)  
@a = 1  
instance\_eval &block  
end  
end

@a = 2  
C.new.x { puts @a } # 1, where a visual parse might suggest 2

Those are the things that come to mind, along with the usual eval  
concerns in cases where you’re giving instance\_eval a string.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)
