Posts Tagged jruby

Using Java in Rails

Tuesday, November 23rd, 2010

Little Nick just found out that it is so easy to use Java in a Ruby and Rails environment.

We were using Jruby 1.5 or something and Rails 3, having a file Phonetic.jar which provided a static encode method that we needed in a model.

I simply moved the file to lib/java/phonetic.jar and extended the model like this.

extend Java
require "java/phonetic.jar"
 
class Whatever < ActiveRecord::Base
  # ..
  def encrypt(string)
    Java.Phonetic.encode(string)
  end

It’s so awesome that you simply can work with Java classes in your Ruby code.

Yeah, I know, all of you guys did that like 5 years ago, but I am still amazed and wanted to share that experience.

Hooray for the Jruby!