Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Binding#eval
binding.eval(string [, filename [,lineno]]) → obj
proc.c
View on GitHub ↗
static VALUE
bind_eval(int argc, VALUE *argv, VALUE bindval)
{
VALUE args[4];
rb_scan_args(argc, argv, "12", &args[0], &args[2], &args[3]);
args[1] = bindval;
return rb_f_eval(argc+1, args, Qnil /* self will be searched in eval */);
}
Evaluates the Ruby expression(s) in string, in the binding’s context. If the optional filename and lineno parameters are present, they will be used when reporting syntax errors.
Example
def get_binding(param) binding end b = get_binding("hello") b.eval("param") #=> "hello"
Ruby version changes
-
3.0
-
Binding#evalwhen called with one argument will use “(eval)” for__FILE__and 1 for__LINE__in the evaluated code. [Bug #4352] [Bug #17419]
-