Forum: Ruby Write up on hazards of instance_eval?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
itsme213 (Guest)
on 2006-03-04 02:41
(Received via mailing list)
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.
unknown (Guest)
on 2006-03-04 06:00
(Received via mailing list)
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
unknown (Guest)
on 2006-03-04 06:42
(Received via mailing list)
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
This topic is locked and can not be replied to.