Created
May 22, 2012 08:10
-
-
Save anonymous/2767504 to your computer and use it in GitHub Desktop.
JRuby class and Java Scripting Engine unexpected results
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 javax.script.Invocable; | |
import javax.script.ScriptEngine; | |
import javax.script.ScriptEngineManager; | |
/* | |
* http://stackoverflow.com/questions/10692951/jruby-class-and-java-scripting-engine-unexpected-results | |
*/ | |
public class App { | |
public static void main( String[] args ) throws Exception{ | |
ScriptEngineManager sm = new ScriptEngineManager(); | |
ScriptEngine se = sm.getEngineByName("jruby"); | |
StringBuilder sb = new StringBuilder(); | |
sb.append("class Test\n"); | |
sb.append(" attr_accessor :a, :b\n"); | |
sb.append(" def str\n"); | |
sb.append(" \"#{a}, #{b} is a test.\"\n"); | |
sb.append(" end\n"); | |
sb.append("end\n"); | |
sb.append("o = Test.new\n"); | |
sb.append("o.a = 'A'\n"); | |
sb.append("o.b = 'B'\n"); | |
sb.append("o"); | |
Object o = se.eval(sb.toString()); | |
System.out.println( ((Invocable) se).invokeMethod(o, "str")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment