### Archive

Archive for the ‘jruby’ Category

## Using rails.vim with JRuby

I spent this evening fiddling around with rails.vim and JRuby.  I have some Java code that I’d like to use with Rails, so I’m using JRuby (also, JRuby seems to hate Windows slightly less than MRI).  Anyway, this is really about using vim to edit Rails code. I installed rails.vim and found that it was hard-coded to use plain old Ruby. Assuming that jruby.bat (or jruby, I guess) is on your system path, all you need to do is modify the app_ruby_shell_command in autoload/rails.vim like this:

function! s:app_ruby_shell_command(cmd) dict abort
if self.path() =~ '://'
return "jruby.bat ".a:cmd
else
return "jruby.bat -C ".s:rquote(self.path())." -S ".a:cmd
endif
endfunction

Basically, just change “ruby” to “jruby.bat” and add the -S parameter.

The -S flag is important. Otherwise, it seems JRuby’s implementation of the -C flag is a little buggy. For example, almost any script/ command will fail:

>jruby -C c:\path\to\rails\app script/generate
C:/Program Files/jruby-1.3.1/bin/../lib/ruby/1.8/pathname.rb:711:in relative_path_from':
different prefix: "C:/" and "C:\\path/to/rails/app" (ArgumentError)
from C:/Program Files/jruby-1.3.1/lib/ruby/gems/1.8/gems/rails-2.3.3/lib/rails_generator/lookup.rb:110:in
use_component_sources!'

Everything seems to be working well now. Yay.

app_ruby_shell_command
Categories: jruby Tags:

## JRuby … Testify!

Recently on the JRuby mailing list, a call went out for all the crypto-jrubyists to come out of their holes and tell the world about their experience with JRuby. I believe the call was inspired by this blog post by Brian Tatnall. So, I figured I’d throw in my two cents.

Unlike Brian, I am not someone who “works with Rails every day”.  In my job, I do a lot of Java, C++, Tcl, and maybe some Python when I get lucky. I’ve heard a lot of good things about Ruby, but have never had the chance to try it out on a project. Adding yet another language to one of our systems would just be mean.  So I tried Ruby a few times, liked it, and then forgot it.

### The Opportunity

Anyway, a few months ago an odd (for my company) project came along that required us to build a small webapp that does custom indexing and searching of a document repository.  I should mention that not only am I a total Ruby noob, but I’m also a total webapp noob… In any case, this seemed like the perfect chance to learn something new and a colleague of mine had been playing around with Rails at home so we at least had that.

Some initial work had already been done using Lucene in Java and after talking with the customer, we learned that they wanted to deploy the app on Tomcat 5.5. Enter JRuby.

### The Development

We decided to go with JRuby (version 1.1.3 at the time). Since JRuby allows Ruby code to call into existing Java libraries, I decided to stick with the existing Lucene code in Java. Thus, I built a back-end indexer and query API in Java, while we built the web front-end in JRuby and Rails.

Honestly, it was almost too easy. Rails makes building the webapp sickeningly easy, especially for something as straightforward as what we were doing. Calling the Java-based query interface I had built was also a snap. I just imported my jar and started calling methods. The conversion from Java objects to Ruby objects was mostly seamless. I think I only had to resort to a couple of map calls to turn lists returned by the query interface into objects that Rails could use in its view templates.

I should note that jruby is actively supported by NetBeans and apparently in Aptana on Eclipse as well.  For everything I was doing with Rails, I stuck with vim and the command-line. Rails already does way more magic than I understand and an IDE would just add one more layer to my ignorance.

I think the only “hack” I ended up with was a static list of query objects stored in Java and accessed from JRuby. Since the Rails controllers are stateless, I couldn’t figure out where I should store global app data, so for expediency, I put them in a global map. I’m very bad. I chalk this up to total ignorance of Ruby and Rails (more on this below).

### The Deployment

We finished the first phase of development way under budget largely thanks to JRuby and Rails. Yay!  This turns out to have been a good thing, because we needed that extra time when we went to deploy. Recall that we were going to deploy to the customer’s Tomcat server.  Luckily, the Warbler tool makes this process mostly painless. Given a Rails app, Warbler packages everything up and generates a war file.

I had a few issues with Warbler. First, there was some inconsistency in what command to actually run. In some documentation, it said to “warble”, when the command was really “warbler”, or vice versa, I don’t remember. Second, it took me a little while to figure out the right method of specifying the gems and Java libraries that my app depended on. I recall spending a day fiddling with Tomcat, trying to make sure my jars were discovered as well as the right JDBC ActiveRecord adapter, etc. I think this again has more to do with my ignorance than any actual problems with Warbler.

So I set up a box that mirrored the customers setup and documented the deployment process. Unfortunately, the customer was in another state which meant the install was actually going to be done by a coworker in that state who knew even less about this stuff than me. It took her a day or two of phone calls clarifying everything I’d missed in my installation guide, but eventually, everything worked and we had a working webapp!

I should also mention that more than half of the install time was spent on issues with my Java indexer. JRuby wasn’t really much of a problem.

## The Conclusion and p.s. I Don’t Know Ruby

So, the project was really a success. Even with the deployment hassles, we were still under budget, impressed the customer and were able to get follow-on funding to add features. Thanks JRuby!

However, while I was working on the project, I noticed a phenomenon I had read about here. I had built and deployed Ruby on Rails app and can safely say that I barely know Ruby any better than I did when I started. Rails does so much work for you that you can get by without really knowing any Ruby at all. That’s a little sad, since projects like this are usually the only time I get to really learn a new language in any depth. Maybe I need to start with a pure Ruby app. I’ve been thinking that a lot of the Swing UI code I’ve been doing for the jsoar debugger would be quicker and more educational if it was done in jruby, or one of the other JVM-based languages that have been multiplying like rabbits lately. We’ll see.

## Others

Here are other JRuby testimonials and I’ve come across:

Categories: Tags:

## Nice intro to JRuby/Java integration

November 25th, 2008 1 comment

Mikio Braun has written a good summary on calling Java code from JRuby. A lot of this material is covered on the JRuby Wiki, but Mikio adds some much needed exposition. Thanks Mikio!

Categories: Tags: