Skip to content

Commit 887913e

Browse files
committed
[DOC] Improve docs for eval
1 parent 7c75dbe commit 887913e

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

vm_eval.c

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,21 +2031,38 @@ eval_string_with_scope(VALUE scope, VALUE src, VALUE file, int line)
20312031
}
20322032

20332033
/*
2034-
* call-seq:
2035-
* eval(string [, binding [, filename [,lineno]]]) -> obj
2034+
* call-seq:
2035+
* eval(string, binding = nil, filename = default_filename, lineno = 1) -> obj
20362036
*
2037-
* Evaluates the Ruby expression(s) in <em>string</em>. If
2038-
* <em>binding</em> is given, which must be a Binding object, the
2039-
* evaluation is performed in its context. If the optional
2040-
* <em>filename</em> and <em>lineno</em> parameters are present, they
2041-
* will be used when reporting syntax errors.
2037+
* Evaluates the Ruby expression(s) in +string+. Returns the result of the last
2038+
* expression.
20422039
*
2043-
* def get_binding(str)
2044-
* return binding
2045-
* end
2046-
* str = "hello"
2047-
* eval "str + ' Fred'" #=> "hello Fred"
2048-
* eval "str + ' Fred'", get_binding("bye") #=> "bye Fred"
2040+
* str = "Hello"
2041+
* eval("str + ' World'") # => "Hello World"
2042+
*
2043+
* If +binding+ is given, which must be a Binding object, the
2044+
* evaluation is performed in its context. Otherwise, the
2045+
* evaluation is performed in the context of the caller.
2046+
*
2047+
* def get_binding(str) = binding
2048+
* str = "Hello"
2049+
* eval("str + ' World'", get_binding("Bye")) # => "Bye World"
2050+
*
2051+
* If the optional +filename+ is given, it will be used as the
2052+
* filename of the evaluation (for <tt>__FILE__</tt> and errors).
2053+
* Otherwise, it will default to <tt>(eval at __FILE__:__LINE__)</tt>
2054+
* where <tt>__FILE__</tt> and <tt>__LINE__</tt> are the filename and
2055+
* line number of the caller, respectively.
2056+
*
2057+
* eval("puts __FILE__") # => "(eval at test.rb:1)"
2058+
* eval("puts __FILE__", nil, "foobar.rb") # => "foobar.rb"
2059+
*
2060+
* If the optional +lineno+ is given, it will be used as the
2061+
* line number of the evaluation (for <tt>__LINE__</tt> and errors).
2062+
* Otherwise, it will default to 1.
2063+
*
2064+
* eval("puts __LINE__") # => 1
2065+
* eval("puts __LINE__", nil, "foobar.rb", 10) # => 10
20492066
*/
20502067

20512068
VALUE

0 commit comments

Comments
 (0)