mod.module_eval {|| block } â obj
Instance Public methods
Evaluates the string or block in the context of mod, except that
when a block is given, constant/class variable lookup is not affected. 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.
1 2 3 4 5 6 | 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:
1 2 3 | Hello there! dummy: 123 :in `module_eval': undefined local variable or method `code' for Thing: Class |
Please login to continue.