Rhinoを使ってJRubyからJavaScriptを呼び出す簡単なサンプル
JavaのをJrubyに移植したら、ソースボリュームが半分になった。ウケる。
ちなみに、Jruby on Rails on GAE の環境です。
js.jarは、WEB-INF/lib/ 配下に配置。
class SAMPLE import 'org.mozilla.javascript.Context' import 'org.mozilla.javascript.Function' import 'org.mozilla.javascript.Scriptable' include_class('java.lang.Object') {|package,name| "J#{name}"} def sample2(arg) context = Context.enter() scope = Scriptable.new scope = context.initStandardObjects() context.evaluateString(scope, "function helloworld(arg){return 'hello ' + arg;}", "<cmd>", 1, nil) fObj = scope.get("helloworld", scope) functionArgs = [] functionArgs << arg result = fObj.call(context, scope, scope, functionArgs.to_java) report = "helloworld('world') = " + Context.toString(result) return report end end