Skip to content

It occurred to me after posting last night that JRuby can be installed via RVM as well:

  $ rvm install jruby

As you would expect, RVM makes JRuby look like your normal Ruby:

  $ rvm use jruby
  Using /Users/craigc/.rvm/gems/jruby-1.5.6
  $ ruby -v
  jruby 1.5.6 (ruby 1.8.7 patchlevel 249) (2010-12-03 9cf97c3) ¬
  (Java HotSpot(TM) 64-Bit Server VM 1.6.0_24) [x86_64-java]

Wait – 1.8.7? What is this, 2008?

As it turns out, JRuby doesn’t officially implement Ruby 1.9 in version 1.5.6. 1 The key word is “officially”, as it’s possible to enable 1.9 support with an environment variable:

  $ export JRUBY_OPTS="--1.9"
  $ ruby -v
  jruby 1.5.6 (ruby 1.9.2dev trunk 24787) (2010-12-03 9cf97c3) ¬
  (Java HotSpot(TM) 64-Bit Server VM 1.6.0_24) [x86_64-java]

Just as MacRuby is Ruby tied to Cocoa, so JRuby is Ruby tied to Java:

  $ irb
  jruby-1.5.6 :001 > include Java
   => Object
  jruby-1.5.6 :002 > StringBuffer = java.lang.StringBuffer
   => Java::JavaLang::StringBuffer
  jruby-1.5.6 :003 > s = StringBuffer.new
   => #<Java::JavaLang::StringBuffer:0x3fc66ec7>
  jruby-1.5.6 :004 > s.append "Hello"
   => #<Java::JavaLang::StringBuffer:0x3fc66ec7>
  jruby-1.5.6 :005 > s.append " world!"
   => #<Java::JavaLang::StringBuffer:0x3fc66ec7>
  jruby-1.5.6 :006 > s.toString
   => "Hello world!"

  1. JRuby 1.6.0, which should be out Any Day Now, is supposed to be fully compatible with Ruby 1.9.2.