在Ruby运行JavaScript代码:ExecJS
jopen
11年前
ExecJS让您可以在Ruby中运行JavaScript代码。它自动选择最佳运行可用来评估你的JavaScript程序,然后将结果作为一个Ruby对象返回。
ExecJS支持以下运行期:
- therubyracer - Google V8 embedded within Ruby
- therubyrhino - Mozilla Rhino embedded within JRuby
- Node.js
- Apple JavaScriptCore - Included with Mac OS X
- Microsoft Windows Script Host (JScript)
require "execjs" require "open-uri" source = open("http://jashkenas.github.com/coffee-script/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};"