Created
August 12, 2016 04:59
-
-
Save chadselph/0be497f37b16f1b01009836686e3875c to your computer and use it in GitHub Desktop.
JRuby instance_evals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jruby.embed.ScriptingContainer; | |
public class RubyTest { | |
public static void main(String[] args) { | |
ScriptingContainer container = new ScriptingContainer(); | |
// From a string | |
Object myclassString = container.runScriptlet("" + | |
"class MyClass\n" + | |
" def parse_stuff(body)\n" + | |
" instance_eval(body)\n" + | |
" end\n" + | |
"end\n" + | |
"MyClass.new"); | |
container.callMethod(myclassString, "parse_stuff", | |
"Kernel.eval 'def abc() ::Kernel.puts 123 end'\n" + | |
"abc"); | |
} | |
} | |
/* | |
Output: | |
NameError: undefined local variable or method `abc' for #<MyClass:0x7bac686b> | |
<eval> at (eval):2 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In irb...