Method: Module#module_eval

Defined in:
eval.c

#class_eval(string[, filename [, lineno]]) ⇒ Object #module_eval { ... } ⇒ Object

Evaluates the string or block in the context of mod. This can be used to add methods to a class. module_eval returns the result of evaluating its argument. The optional filename and lineno parameters set the text for error messages.

class Thing
end
a = %q{def hello() "Hello there!" end}
Thing.module_eval(a)
puts Thing.new.hello()
Thing.module_eval("invalid code", "dummy", 123)

produces:

Hello there!
dummy:123:in `module_eval': undefined local variable
    or method `code' for Thing:Class

Overloads:

  • #class_eval(string[, filename [, lineno]]) ⇒ Object

    Returns:

  • #module_eval { ... } ⇒ Object

    Yields:

    Returns:



6912
6913
6914
# File 'eval.c', line 6912

VALUE
rb_mod_module_eval(argc, argv, mod)
int argc;