public Method

Kernel.eval(...)

eval(string [, binding [, filename [,lineno]]])   obj

Evaluates the Ruby expression(s) in string. If binding is given, the evaluation is performed in its context. The binding may be a Binding object or a Proc object. If the optional filename and lineno parameters are present, they will be used when reporting syntax errors.

def getBinding(str)
  return binding
end
str = "hello"
eval "str + ' Fred'"                      #=> "hello Fred"
eval "str + ' Fred'", getBinding("bye")   #=> "bye Fred"

Source Code

/*
*  call-seq:
*     eval(string [, binding [, filename [,lineno]]])  => obj
*  
*  Evaluates the Ruby expression(s) in <em>string</em>. If
*  <em>binding</em> is given, the evaluation is performed in its
*  context. The binding may be a <code>Binding</code> object or a
*  <code>Proc</code> object. If the optional <em>filename</em> and
*  <em>lineno</em> parameters are present, they will be used when
*  reporting syntax errors.
*     
*     def getBinding(str)
*       return binding
*     end
*     str = "hello"
*     eval "str + ' Fred'"                      #=> "hello Fred"
*     eval "str + ' Fred'", getBinding("bye")   #=> "bye Fred"
*/

static VALUE
rb_f_eval(argc, argv, self)
   int argc;
   VALUE *argv;
   VALUE self;
{
   VALUE src, scope, vfile, vline;
   char *file = "(eval)";
   int line = 1;

   rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline);
   if (ruby_safe_level >= 4) {
       StringValue(src);
       if (!NIL_P(scope) && !OBJ_TAINTED(scope)) {
           rb_raise(rb_eSecurityError, "Insecure: can't modify trusted binding");
       }
   }
   else {
       SafeStringValue(src);
   }
   if (argc >= 3) {
       StringValue(vfile);
   }
   if (argc >= 4) {
       line = NUM2INT(vline);
   }

   if (!NIL_P(vfile)) file = RSTRING(vfile)->ptr;
   if (NIL_P(scope) && ruby_frame->prev) {
       struct FRAME *prev;
       VALUE val;

       prev = ruby_frame;
       PUSH_FRAME();
       *ruby_frame = *prev->prev;
       ruby_frame->prev = prev;
       val = eval(self, src, scope, file, line);
       POP_FRAME();

       return val;
   }
   return eval(self, src, scope, file, line);
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.