Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Kernel#eval

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

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.

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

Ruby version changes

  1. 3.0
    • Kernel#eval when called with two arguments will use “(eval)” for __FILE__ and 1 for __LINE__ in the evaluated code. [Bug #4352]

See also #__dir__ #exec #Complex #exit also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0