Project

runjs

0.0
No commit activity in last 3 years
No release in over 3 years
You can run JavaScript code from Ruby with RunJS. The supported JavaScript engines are: Node, V8, TheRubyRacer, JavaScriptCore and JScript.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

RunJS

With RunJS you can run JavaScript code from Ruby.

require 'runjs'
puts RunJS.run('return "Hello World";')

Installation

$ gem install runjs

RunJS depends on the json library. Starting with Ruby version 1.9 this is included, but on Ruby 1.8 you have to install the json gem. If this causes any problems, install the json_pure gem instead.

$ gem install json || gem install json_pure

JavaScript Runtimes

Supported runtimes

RunJS will automatically select the first available runtime from the list above.

Deprecated runtimes

To use TheRubyRhino or SpiderMonkey you have to set the runtime manually.

Set the runtime

You can control which runtime RunJS uses in your Ruby code.

RunJS.runtime = RunJS::Node

Or with the RUNJS_RUNTIME environment variable.

$ export RUNJS_RUNTIME=Node

Examples

require 'runjs'

RunJS.run('return 2 + 2;')                     # => 4
RunJS.call('Math.sqrt', 25)                    # => 5
RunJS.apply('Array.prototype.slice', '"cat"')  # => ["c", "a", "t"]

Compiling CoffeeScript

require 'runjs'
require 'open-uri'

url = 'http://jashkenas.github.com/coffee-script/extras/coffee-script.js'
compiler = open(url).read

coffee = 'alert yes'
options = { header: true, bare: true }
RunJS.context(compiler).apply('CoffeeScript.compile', 'CoffeeScript', coffee, options)

# => // Generated by CoffeeScript 1.6.3
# => alert(true);

Credits

Thanks to Sam Stephenson, Joshua Peek and the other contributors to ExecJS. Although none of the code is directly copied, the API and code structure is almost identical. While coding, the ExecJS code was consulted all the time, and RunJS ended up like a rewrite of ExecJS.

License

Copyright (c) 2013 AS Harbitz.

RunJS is released under the MIT License.