JSRuby

JSRuby is a Ruby interpreter impremented in JavaScript.

Requires: IE6, Firefox2, Opera9, Safari3, SpiderMonkey, Rhino

JSRuby Latest Version (For Developers)

svn co http://svn.coderepos.org/share/lang/javascript/jsruby/trunk/

"./trunk/check.html" is a simple check form of JSRuby. You can get a parse tree of ruby script and execute it. You can also try jsirb(interactive JSRuby).

For making the release package, you execute "make.sh".

cd ./trunk
bash ./make.sh

"jsruby.js" will be generated on "./dist/" .

Functional Test

In JSRuby's functional test, you use JsUnit.

You get "jsunit2.2alpha11.zip" and extract all from it at "./trunk/", so "./jsunit/" directory will be made.

cd ./trunk
unzip jsunit2.2alpha11.zip

Open "./jsunit/testRunner.html" by a web browser.

e.g. if you extract on c:/jsruby/trunk/

file:///c:/jsruby/trunk/jsunit/testRunner.html?testPage=c:/jsruby/trunk/testsuite.html&autoRun=true

Sample Code

Simplely execute a Ruby code.

var ruby = new RubyEngine.Interpreter();
ruby.exec(" (1..10).each{ |x| puts x*x } ");
alert(ruby.stdout);

Invoke a user defined Ruby method from Javascript.

<script type="text/ruby">
def fib(n)
  if n<2
    n
  else
    fib(n-1)+fib(n-2)
  end
end
</script>

<script language="JavaScript" type="text/javascript">
  var ruby = new RubyEngine.Interpreter();
  ruby.exec( RubyEngine.Util.getRubyScript() );  // execute script of type="text/ruby"
  alert( ruby.fib(10) );  // => 55
</script> 

Use jQuery in JSRuby.

<script type="text/ruby">
def getfooter
  $jquery('div.footer')[0].innerHTML
end
</script>

<script type="text/javascript" src="jquery-*.*.*.js"></script>
<script type="text/javascript">
  var ruby = new RubyEngine.Interpreter();
  ruby.exec( RubyEngine.Util.getRubyScript() );  // execute script of type="text/ruby"
  ruby.put( "$jquery", $ )
  alert( ruby.getfooter() );
</script> 

<div class="footer"> (content of footer) </div>

Supported Features (JSRuby 0.1)

  • if ~ elsif ~ else ~ end
  • def
  • puts, p
  • Array#<<
  • Array#[], []=
  • Array#each
  • Array#inject
  • Array#join
  • Array#length
  • Array#member?
  • Array#new
  • Array#push
  • Array#reverse
  • Numeric#+, -, *, /, %, **, ==, <=>
  • Numeric#chr
  • Numeric#to_s
  • Numeric#upto
  • String#+, *, ==
  • String#[], []=
  • String#center
  • String#length
  • String#reverse
  • String#to_i
  • Range#each
  • Extensional:
    • Handling Javascript object
    • $window, $document
    • Calling user defined Ruby method from Javascript

See Also