Alex Rivera | Logout

Is 'eval' supposed to be nasty?

Asked 2009-03-12T04:55:03.003
27

I have been using the eval method in ruby many times. But I have heard people saying eval is nasty. When asked, why and how, I could never get a convincing reason not to use it. Are they really nasty? and if so, in what way? What are possible "safer" options to eval?

Edit
Report

1 Answer

12

In Ruby there are several gimmicks that might be more appropriate than eval():

  1. There is #send which allows you to call a method whose name you have as string and pass parameters to it.
  2. yield allows you to pass a block of code to a method which will be executed in the context of the receiving method.
  3. Often the simple Kernel.const_get("String") is sufficient to get the class whose name you have as string.

I think I am not able to explain them properly in detail, so I just gave you the hints, if you're interested you'll google.

answered 2009-03-12T17:07:17.833

Your Answer