ExecJS在 Ruby 中执行 JS 代码
ExecJS 可以让你在 Ruby 中运行 JavaScript 代码。它会自动选择最佳的运行环境并执行你的 JavaScript 程序,并将结果赋值给 Ruby 对象。
ExecJS 支持如下平台
-
therubyracer - Google V8 embedded within Ruby
-
therubyrhino - Mozilla Rhino embedded within JRuby
-
Apple JavaScriptCore - Included with Mac OS X
-
Microsoft Windows Script Host (JScript)
示例代码:
require "execjs"
require "open-uri"
source = open("http://coffeescript.org/extras/coffee-script.js").read
context = ExecJS.compile(source)
context.call("CoffeeScript.compile", "square = (x) -> x * x", bare: true)
# => "var square;\nsquare = function(x) {\n return x * x;\n};"评论
