Working with JRuby

Posted on Thursday 08 July, 2010 in programming, jruby, RMV, and NailGun

It was difficult to get an effective work flow when we first chose to use jruby for part of our application as nothing was integrated and everything ran a bit slowly. With the help of RVM and Nailgun we managed evolve something that made development almost as easy as using MRI.

Automatically switch to JRuby

As you switching back and forth between projects using different ruby version it quickly becomes a pain having to manually specify the correct one. The project .rvmrc removes the need to do this as when “cd” into your project directory it automatically switches to the correct ruby version. The example below creates a .rvmrc file for a project that uses jruby with the spreadsheets gemset.

local$ echo “rvm jruby@spreadsheets” > .rvmrc

This works when using ruby from the shell but unfortunately TextMate will still be unable run any of your projects files or tests but there is easy to fix.

Configure TextMate’s TM_RUBY variable

RVM again helps by allowing you to create a wrapper script that you can configure TextMate to use instead of the default ruby. To create a script that uses jruby and the same gemset as above run the following command.

local$ rvm —wrapper jruby@spreadsheets textmate

A wrapper script is then created in ~/.rvm/bin/textmate_ruby. Now in TextMate you can create a TM_RUBY shell variable (Textate > Preferences > Shell Variables) and point it at the fully qualified file path. Now cmd+r will run any files or tests form from TextMate again.

This would have been enough it it were not for the JVM startup time. Each time you have to wait those few extra seconds before it your test run it seems longer and longer.

Speeding up Tests

The best solution I for getting the tests to run faster is to drop to the command line and use Nailgun to cut out the start up time. Fire it up with:

local$ ruby —ng-server &

Now you can run your tests with the —ng flag to cut out the start up time (well after the first run anyway). For example if you were using RSpec then simply run:

ruby —ng -S spec path/to/your_spec.rb

If you just want to run a single test then just specify a line number:

ruby —ng -S spec path/to/your_spec.rb -l 12


blog comments powered by Disqus


A blog by Michael Jones, a developer currently working at forward, whose interests include: web technologies; ruby; functional programming; design and making the perfect cup of filter coffee.

Subscribe by RSS