Skip to content

moowahaha/ruby_inline_javascript

Repository files navigation

inline_javascript

DESCRIPTION:

Call JavaScript methods from Ruby using either InlineJavaScript or, more directly, InlineJavaScript::V8Wrapper.

FEATURES/PROBLEMS:

  • InlineJavaScript supports calling methods with multiple parameters of Fixnum (integer), String, Hash, Array or any other class that supports the “.to_json” method (as provided by the “json” gem).

  • InlineJavaScript supports JavaScript return values of type String, Integer, Hash, Array or any other type supported by JavaScript’s “JSON.stringify()” method.

  • InlineJavaScript::V8Wrapper only currently supports functions returning basic scalars, that it then turns into strings.

SYNOPSIS:

To call a JavaScript function as if it was Ruby:

require 'inline_java_script'

js = InlineJavaScript.new('function my_add (a, b) { return a+b}')
js.my_add(1, 2) #=> 3

Using more complex types:

js = InlineJavaScript.new('function my_concat (a) { return {concat_string: a[0] + " " + a[1]} }')
js.my_concat(['hello', 'world']) #=> {"concat_string" => "hello world"}

To call the V8 engine directly (no smarts):

require 'inline_java_script/v8_wrapper'

wrapper = InlineJavaScript::V8Wrapper.new
wrapper.execute('1 + 2') #=> "3"

An instance of the V8Wrapper maintains state:

wrapper = InlineJavaScript::V8Wrapper.new
wrapper.execute('var e = "monster"')
wrapper.execute('e') #=> "monster"

# ... But I cannot access another instance...
InlineJavaScript::V8Wrapper.new.execute('e') #=> SyntaxError

REQUIREMENTS:

  • Google’s libv8. I installed this using Homebrew (github.com/mxcl/homebrew) by running “brew install v8” on OSX. libv8 is also available for other operating systems through various means (e.g. yum install libv8-dev).

INSTALL:

  • gem install inline_javascript

LICENSE:

(The MIT License)

Copyright © 2011 Stephen Hardisty.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Inline JavaScript in Ruby using V8

Resources

Stars

Watchers

Forks

Packages

No packages published