Skip to content

Commit c1f48f4

Browse files
committed
[DOC] Improve docs for Module#module_eval
1 parent 9bd9f26 commit c1f48f4

1 file changed

Lines changed: 56 additions & 19 deletions

File tree

vm_eval.c

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,29 +2392,66 @@ rb_obj_instance_exec(int argc, const VALUE *argv, VALUE self)
23922392

23932393
/*
23942394
* call-seq:
2395-
* mod.class_eval(string [, filename [, lineno]]) -> obj
2396-
* mod.class_eval {|mod| block } -> obj
2397-
* mod.module_eval(string [, filename [, lineno]]) -> obj
2398-
* mod.module_eval {|mod| block } -> obj
2395+
* class_eval(string, filename = nil, lineno = 1) -> obj
2396+
* class_eval { |mod| ... } -> obj
2397+
* module_eval(string, filename = nil, lineno = 1) -> obj
2398+
* module_eval { |mod| ... } -> obj
23992399
*
2400-
* Evaluates the string or block in the context of _mod_, except that when
2401-
* a block is given, constant/class variable lookup is not affected. This
2402-
* can be used to add methods to a class. <code>module_eval</code> returns
2403-
* the result of evaluating its argument. The optional _filename_ and
2404-
* _lineno_ parameters set the text for error messages.
2400+
* Evaluates the +string+ or block in the context of +self+.
2401+
* Returns the result of the last expression.
24052402
*
2406-
* class Thing
2407-
* end
2408-
* a = %q{def hello() "Hello there!" end}
2409-
* Thing.module_eval(a)
2410-
* puts Thing.new.hello()
2411-
* Thing.module_eval("invalid code", "dummy", 123)
2403+
* When +string+ is given, evaluates the given string in the
2404+
* context of +self+:
24122405
*
2413-
* <em>produces:</em>
2406+
* class Foo; end
24142407
*
2415-
* Hello there!
2416-
* dummy:123:in `module_eval': undefined local variable
2417-
* or method `code' for Thing:Class
2408+
* Foo.module_eval("def greeting = puts 'hello'")
2409+
*
2410+
* Foo.new.greeting # => "hello"
2411+
*
2412+
* If the optional +filename+ is given, it will be used as the
2413+
* filename of the evaluation (for <tt>__FILE__</tt> and errors).
2414+
* Otherwise, it will default to <tt>(eval at __FILE__:__LINE__)</tt>
2415+
* where <tt>__FILE__</tt> and <tt>__LINE__</tt> are the filename and
2416+
* line number of the caller, respectively:
2417+
*
2418+
* class Foo; end
2419+
*
2420+
* Foo.module_eval("puts __FILE__") # => "(eval at ../test.rb:3)"
2421+
* Foo.module_eval("puts __FILE__", "foobar.rb") # => "foobar.rb"
2422+
*
2423+
* If the optional +lineno+ is given, it will be used as the
2424+
* line number of the evaluation (for <tt>__LINE__</tt> and errors).
2425+
* Otherwise, it will default to 1:
2426+
*
2427+
* class Foo; end
2428+
*
2429+
* Foo.module_eval("puts __LINE__") # => 1
2430+
* Foo.module_eval("puts __FILE__", nil, 10) # => 10
2431+
*
2432+
* When a block is given, evaluates the block in the context
2433+
* of +self+:
2434+
*
2435+
* class Foo; end
2436+
*
2437+
* Foo.module_eval do
2438+
* def greeting = puts "hello"
2439+
* end
2440+
*
2441+
* Foo.new.greeting
2442+
*
2443+
* However, constant and class variable lookup differs between
2444+
* +string+ and block. When +string+ is given, contant and class
2445+
* variables are looked up in the context of +self+. When a block
2446+
* is given, the context of the lookup is not changed:
2447+
*
2448+
* class Foo
2449+
* GREETING = "hello"
2450+
* end
2451+
*
2452+
* Foo.module_eval("puts GREETING") # => "hello"
2453+
*
2454+
* Foo.module_eval { puts GREETING } # => NameError: uninitialized constant GREETING
24182455
*/
24192456

24202457
static VALUE

0 commit comments

Comments
 (0)