Rhinoを使ってJRubyから外部JavaScriptファイルを呼び出す簡単なサンプル
次は外部JavaScriptファイルの読み込み。
test.js は、RAILSアプリのルート直下に配置。appディレクトリの上。
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}"} include_class('java.io.FileReader') {|package,name| "J#{name}"} def sample3(arg) reader = JFileReader.new("test.js"); context = Context.enter() scope = Scriptable.new scope = context.initStandardObjects() context.evaluateReader(scope, reader, "<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