Write up on hazards of instance_eval?


#1

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.


#2

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.

:wink:

-a


#3

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)

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