Skip to main content

Getting Started with JRuby - Tutorial at RailsConf

Posted by arungupta on May 17, 2007 at 1:04 PM PDT
Earlier today, I attended
Charles Nutter
and
Thomas Enebo "Your
First Day with JRuby on Rails
" tutorial at
Rails Conf 2007.
The key message is Ruby as the programming language and Java for the
platform and libraries provides the best of both worlds to developers.

 

JRuby is 100% pure-Java implementation of the
Ruby programming language. The goal is to use the power of Ruby programming
language and leverage the power of Java platform. Read a more detailed tutorial

here
. All Ruby scripts work with JRuby instead of the original C-based
implementation. Also the goal is NOT an attempt to alter Ruby or add
incompatible features. You can download
JRuby implementation, unzip it and
set JRUBY_HOME to the installation directory. Here is a sample JRuby code:



1. require 'java'

2. include_class 'java.lang.ArrayList'

3. include_class 'javax.swing.JFrame'

4.

5. list  = ArrayList.new

6. frame = JFrame.new("Ruby SWINGS!")

7. list << frame

8. list.each { |f| f.set_size(200, 200) }

9. puts frame.title

The code above shows how Ruby and Java code can be mixed with each other.
Line 1 includes Java support. Line 2 imports Java classes. Line 5 and 6 creates
a new variables using a mix of Java and Ruby syntax. Line 7 and 8 uses the
instance variables in Ruby syntax. Line 9 prints the title of the frame. If you
run this code using JRuby interpreter then you get:



%JRUBY_HOME%/bin/jruby sample1.rb

Ruby SWINGS!

Here are some of the differences (from the original C-based implementation)
in JRuby:

  1. Database support - This is required only if you want to re-use existing
    JDBC connection pool. Otherwise native adapter works fine and is preferred
    because of performance reasons.
    1. Change 'database.yml' from



      development:

        adapter: mysql

        database: HelloWorld_development

        username: root

        password:

        host: localhost



      to




      development:

        adapter: jdbc

        driver: com.mysql.jdbc.Driver

        url: jdbc:mysql://localhost:3306/helloworld_development

        username: root

        password:
    2. Include "require 'jdbc_adapter'" in 'environment.rb'.
    3. Either set CLASSPATH to include JDBC driver (for example,
      MySQL
      Connector/J 5.0.5
      ) or copy into 'work/jruby/lib'
      directory.
  2. No Native extensions (at least, not until ports are available). Today,
    Mongrel,

    Hpricot
    are available. Database support and RMagick are in progress.
  3. Command-line performance - JVM performs much better after the first boot
    so don't expect stellar command-line performance.

Here are some other points that the tutorial talked/showed:

  • Goldspike (nee
    rails-integration
    ) that allows to WAR'up a RoR application.
  • Installation of

    GlassFish V3
    as Ruby gem which will be released soon. This will allow
    the following interaction possible:



    gem install glassfish-rails

    jruby script/server glassfish

    => Starting GlassFish

    => Rails application on http://localhost:8080/8080

    => Rails application on http://localhost:8080/4848

    => Clustering enabled

    => Connecting pooling enabled

    => Load balancing enabled

    => Server Ready.
  • JvYAML is a Java port of
    RbYAML.- Java YAML 1.1 parser and
    emitter
  • Deployment of Mephisto on
    GlassFish V3
  • Powerful editing, code completion, re-factoring capabilities using
    NetBeans
    IDE 6 M9
    . You can find more details
    here.

The biggest pain point from the audience was Rails deployment. With JRuby and
GlassFish, you can continue deploying your apps on
Mongrel. Because of JRuby, it also
allows you to deploy your RoR applications on
GlassFish, open-source,
production-quality, Java EE 5 compatible application server.

BTW, there is supposed to be wireless connectivity through out the conference
but it's very spotty :(

Technorati:
glassfish
rubyonrails
ror

netbeans railsconf

Related Topics >>