ExecJS在 Ruby 中執(zhí)行 JS 代碼
ExecJS 可以讓你在 Ruby 中運行 JavaScript 代碼。它會自動選擇最佳的運行環(huán)境并執(zhí)行你的 JavaScript 程序,并將結(jié)果賦值給 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};"評論
圖片
表情
