eval(p1, p2 = v2, p3 = v3, p4 = v4)
public
Evaluates the Ruby expression(s) in string. If binding is given, which must be a Binding object, the evaluation is performed in its
context. If the optional filename and lineno parameters
are present, they will be used when reporting syntax errors.
def get_binding(str)
return binding
end
str = "hello"
eval "str + ' Fred'"
eval "str + ' Fred'", get_binding("bye")
Show source
VALUE
rb_f_eval(int argc, VALUE *argv, VALUE self)
{
VALUE src, scope, vfile, vline;
const char *file = "(eval)";
int line = 1;
rb_scan_args(argc, argv, "13", &src, &scope, &vfile, &vline);
if (rb_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_PTR(vfile);
return eval_string(self, src, scope, file, line);
}