Archive for the 'JRuby' Category



Interview with Ola Bini - JRuby Core Developer

Thursday 30 August 2007 @ 2:45 pm
Ola Bini

RubyLearning caught up with Ola Bini at Bangalore, India and talked to him about JRuby. Ola provides some insights for easy adoption of JRuby by the large pool of Java and Ruby developers in India.

Hello Ola, and welcome to RubyLearning.com. Tell us something about your background and how you came to JRuby.

Well, I’m a programming language geek and have looked at many languages for a long time. I’ve used LISP since before I can remember, but Java have always been the language that is economically viable to use. But I’m not that fond of the Java language - the platform is very good, but the language… well. So I looked around for something better. I’ve known Ruby for a few years; it’s got some very nice similarities to LISP, Smalltalk and other very nice languages. Finally I found the JRuby project, realized that it would hit my sweet spot perfectly and decided to start helping out on it.

Could you tell us some reasons on why a Ruby developer should start looking into JRuby?

There are several good reasons: most of the problems of Ruby 1.8 are actually resolved by JRuby in one way or the other. You get better threading semantics, native unicode, active development, easier extensions, very good garbage collection - and superb integration with Java libraries. All of these are very nice things for a Ruby developer since they fix deficiencies in the current MRI 1.8.

How should a person working on Ruby approach JRuby?

The first step is just to try your application out on it. If it’s a regular Ruby application that doesn’t use native extensions, the chance is very good that it will just run on JRuby. The next step - after the application runs - is to get ready to integrate some of the Java specific libraries and start using them instead. For example, theres a backend to REXML that uses Java libraries instead; this makes REXML much faster and also safer. It will also add new capabilities to REXML, like validation and so on. And this is with the exact same interface as regular REXML, so you get all this for free, just by moving. The same thing applies for running JRuby on Rails, and using JDBC as a back end connector.

With a stable release now in hand what is your current focus and roadmap for JRuby?

Well, the current focus is on completing the compiler and also working on performance. We are not making huge changes in the functionality right now - instead looking at external libraries that enhance the value of JRuby.

Can you give some examples where JRuby is being persistently used?

Well, ThoughtWorks sells Mingle, which runs on JRuby on Rails. Sun has some JRuby projects. Oracle is working on JRuby projects. And there are many, many smaller projects being developed all around the world.

Can you tell us one area of JRuby wherein you feel there needs to be an improvement?

Well, the 1.0-release was focused on compatibility. The performance was pretty good, but now we really want to surpass MRI in performance. So that’s what we’re working on right now. Aside from that, documentation is sorely needed too. (My book, that comes out Sept. 24th, will probably help some with that.)

Is there a plan to have a developers tool for JRuby?

Well, right now there is a lack of development tools for Ruby itself - many companies are working on it though. These tools will work for JRuby too. I know that NetBeans are planning to add some JRuby specific things to their Ruby tool later on.

Can you highlight on some good mechanisms for deploying JRuby based applications?

That’s kinda hard. If you’re talking about Rails applications, using WAR based deployment to a Java application server is generally the best solution.

How is the performance and scalability aspect of JRuby?

I addressed performance above. Scalability is another thing. Generally JRuby takes more memory than MRI, but usually scales better due to better threading.

Finally, how easy it is to pickup JRuby? Any pointers?

It’s as easy as picking up Ruby. It’s really easy to get started, and if you google for the JRuby wiki, you will find lots of good information for getting started. Once again, my book will help here… :)

Thanks Ola for sharing your views with the RubyLearning.com members.

Technorati Tags: , , , , , ,

Posted by Satish Talim



JRuby Inside

Tuesday 17 July 2007 @ 7:58 am

Peter Cooper has done it again! Peter has just launched JRuby Inside a new “sister” site to his very popular Ruby Inside. JRuby Inside focuses just on JRuby.

About JRuby Inside, Peter says:

JRuby Inside is not just a blog. It collates links from various sources, allows me (as the webmaster) to promote certain ones to posts or to the RSS feed, and even allows regular visitors to submit posts. It’s a more freeform but less demanding site than Ruby Inside, but hopefully will provide just as much value in a smaller niche.

Peter, the very best wishes from the Ruby and Java communities for the success of JRuby Inside.

Technorati Tags: , ,

Posted by Satish Talim



Connect JRuby to MySQL using JDBC

Saturday 5 May 2007 @ 8:05 am

JRuby is a 100% pure-Java implementation of the Ruby programming language that runs in the JVM. MySQL is a one of the most popular open source databases around and is used by many prominent organizations from Yahoo to NASA.

This brief tutorial demonstrates how to install and configure JRuby to connect to the MySQL database. The tutorial is written for beginners who are familiar with Java code and JDBC. Exposure to Ruby will make the syntax easier to follow. You don’t need any specific integrated development environment (IDE) or tool knowledge. Familiarity with a text editor and setting environment variables is required.

The tutorial assumes that your Java, JRuby and MySQL 4.1+ environment is successfully installed and configured.

Creating a Database and User

Again, I am assuming that you have MySQL running and are familiar with the basics. Run the mysql client program from the command line (as shown below) so that we can execute some administration commands.

C:/mysql/bin> mysql –user=root mysql

First create a database called “ruby” within MySQL and the user is root.

mysql> CREATE DATABASE ruby;
Query OK, 1 row affected (0.02 sec)

and check that it has been created using

mysql> SHOW DATABASES;
+————+
| Database   |
+————+
| ruby       |
| mysql      |
| test       |
+————+
3 rows in set (0.01 sec)

Let’s create a table in our database ruby

use ruby;
create table student (id VARCHAR(2), name VARCHAR(20), rank VARCHAR(2));

To check whether the table has been created, type:

show tables;
+———————+
| Tables_in_ruby      |
+———————+
| student             |
+———————+
1 row in set (0.00 sec)

To verify your table, type:

mysql> describe student;
+——–+—————-+——+——+———–+——–+
| Field  | Type           | Null | Key  | Default   | Extra  |
+——–+—————-+——+——+———–+——–+
| id     | char(2)        | YES  |      | NULL      |        |
| name   | varchar(20)    | YES  |      | NULL      |        |
| rank   | char(2)        | YES  |      | NULL      |        |
+——–+—————-+——+——+———–+——–+
3 rows in set (0.04 sec)

Now let us insert 3 records into student table:

mysql> insert into student values (’01′, ‘Peter’, ‘10′);
Query OK, 1 row affected (0.06 sec)

mysql> insert into student values (’02′, ‘Bruce’, ‘08′);
Query OK, 1 row affected (0.01 sec)

mysql> insert into student values (’03′, ‘Pat’, ‘06′);
Query OK, 1 row affected (0.00 sec)

Download type 4 pure JDBC driver

Download mySQL Connector/J Ver 3.1.10 or later open source JDBC driver for the above database. Unzip and Install the pure JDBC driver to C:/default folder ie. C:/mysql-connector-java-3.1.8
Next, copy the file mysql-connector-java-3.1.10-bin.jar to the lib folder of your Java installation and then add this .jar file location to your system environment variable classpath.

Connect to MySQL

Now that you’ve created a database and populated it with some data, your next task is to connect to that database using JRuby. To do so, create a new file named jrubyjdbc.rb. Open this file in a text editor, and enter the lines shown below:

require ‘java’

module JavaLang
  include_package “java.lang”
end

module JavaSql
  include_package ‘java.sql’
end

begin
  JavaLang::Class.forName(”com.mysql.jdbc.Driver”).newInstance
  conn = JavaSql::DriverManager.getConnection(”jdbc:mysql://127.0.0.1:3306/ruby?user=root”);
  stmt = conn.createStatement
  rs = stmt.executeQuery(”select name from student”)
  while (rs.next) do
    puts rs.getString(”name”)
  end
  rs.close
  stmt.close
  conn.close()
rescue JavaLang::ClassNotFoundException
  puts “ClassNotFoundException”
rescue JavaSql::SQLException
  puts “SQLException”
end

The code should work and you should get the output as:

C:/rubyprograms>jruby jrubyjdbc.rb
Peter
Bruce
Pat

Some code explanation

  • Create a Ruby module into which you put all the classes in java.sql. Because you’re using numerous classes from the java.sql package, it
    makes sense to include the whole package. Keep in mind that including the package slows your script. This use of a module is a convenient way to access all of these classes.
  • There can be name clashes between Java class names and Ruby class names. Class is an example of this; Java has java.lang.Class and Ruby also has Class. To resolve this name clash, define a Ruby module that includes the Java class definition.
  • There’s no need to declare the type of the variable named connection. Ruby is a dynamically typed language; therefore types aren’t required for variable declarations.
  • Class.forName and DriverManager.getConnection both throw checked exceptions. In Java code, the equivalent code won’t compile until you do something with those exceptions. Ruby doesn’t have checked exceptions, so you aren’t forced to handle them. However, when writing an application, you generally want to do something with exceptions if you can. Two checked exceptions could arise from your code: ClassNotFoundException and SQLException. Exceptions in Ruby are handled similarly to the Java approach. You have a block starting with begin instead of try and ending with end instead of }. Instead of catch, you use rescue.

Go on! Digg It !

Technorati Tags: , , , , ,

Posted by Satish Talim



Internationalization in JRuby

Monday 30 April 2007 @ 11:15 am

I have been using and teaching Java since 1995. The other day, I was talking to my students about Internationalization in Java.

Internationalization is the process of designing an application so that it can be adapted to various languages and regions without engineering changes.

Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text.

The following is a simple example (in Java) to demonstrate how to internationalize a program so that it displays text messages in the appropriate language. Notice that the text of the messages is not hardcoded.

import java.util.*;
public class InternationalizationEx
{
  public static void main(String[] args)
  {
    String lang, country;
    Locale cLocale;
    ResourceBundle msg;

    lang = new String(”de”);
    country = new String(”DE”);

    cLocale = new Locale(lang, country);
    msg = ResourceBundle.getBundle(”MessagesBundle”, cLocale);

    System.out.println(msg.getString(”greetings”));
    System.out.println(msg.getString(”welcome”));
  }
}

To compile and run the above program, you need these source files:

The internationalized program uses a language and a country code. In the above example the language code is de (German) and the country code is DE (Germany), so the program displays the messages in German:

java InternationalizationEx
Hallo
HeiBen Sie Willkommen nach Indien

If the language code is en (English) and the country code is US (United States), the program displays the messages in English:

java InternationalizationEx
Hello
Welcome to India

The arguments passed to the getBundle method identify which properties file will be accessed. The first argument, MessagesBundle, refers to this family of properties files:

  • MessagesBundle_de_DE.properties
  • MessagesBundle_en_US.properties

The Locale, which is the second argument of getBundle, specifies which of the MessagesBundle files is chosen. When the Locale was created, the language code and the country code were passed to its constructor. Note that the language and country codes follow MessagesBundle in the names of the properties files.

You may be wondering what’s the point of this post and that too about code in Java, whereas RubyLearning.com is a blog that talks about Ruby and Rails!

Internationalization in JRuby

Well, since I am currently exploring JRuby, I wanted to call these Java classes in Ruby using JRuby. In Ruby, one can use Masao Mutoh’s Ruby-GetText-Package to do the same, though personally I find it quite cumbersome to use.

So here’s JRuby to my rescue! The program code in JRuby is:

#javaInternational.rb
require 'java'

module JavaLang
  include_package "java.lang"
end

ResourceBundle = java.util.ResourceBundle
Locale = java.util.Locale

lang = JavaLang::String.new("de")
country = JavaLang::String.new("DE")

cLocale = Locale.new(lang, country)
msg = ResourceBundle.getBundle("MessagesBundle", cLocale)

puts msg.getString("greetings")
puts msg.getString("welcome")

Run the program on a command prompt as follows:

jruby javaInternational.rb

It works! Okay, a little explanation of some of the JRuby code - There can be name clashes between Java class names and Ruby class names. String is an example of this; Java has java.util.String and Ruby also has Kernel::String. To resolve this name clash, define a Ruby module that includes the Java class definition as follows:

module JavaLang
  include_package "java.lang"
end

If you know Java and have started using Ruby, please do explore JRuby - you shall not be disappointed.

Technorati Tags: , , ,

Posted by Satish Talim



JRuby: Caffeinated Ruby

Friday 27 April 2007 @ 11:52 am

Recently, JRuby has been gaining more and more attention in the Java and Ruby communities. Java is a powerful platform and there are millions of lines of Java code being written each month, that the world will have to live with for a long time from now. By leveraging Java the platform with the power of the Ruby programming language, programmers get the best from both worlds. You better not ignore JRuby any more!

What is JRuby?

JRuby is a 100% pure-Java implementation of the Ruby programming language that runs in the JVM. JRuby’s creators, Thomas Enebo and Charles Nutter, have been hired by Sun to work on JRuby full time. The current JRuby release 0.9.9 is fully compatible with Ruby 1.8.5.

Ola Bini says that “JRuby is ready for prime time. Application developers should try their applications on JRuby NOW.”

So, I decided to take a quick look at JRuby and found it very easy to use.

Download and Setup:

  • The JRuby distribution comes as a tar.gz file, namely jruby-bin-0.9.9.tar.gz
  • Uncompress the archive; you should end up with a jruby-0.9.9 folder.
  • In Windows, set the system environment variable JRUBY_HOME to C:\jruby-0.9.9 I am assuming that you have uncompressed JRuby to C:
  • Also, set the system environment variable path to C:\jruby-0.9.9\bin;
  • The JRuby distribution’s bin directory contains the jruby.bat file that is used to run the JRuby interpreter. Run the command jruby -version from the command line to test that the JRuby is working. On my PC, it said:
    ruby 1.8.5 (2007-04-23 rev 3539) [x86-jruby0.9.9]

Where to use JRuby?:

a. JRuby allows Ruby programs to use Java classes. This is a powerful concept that JRuby now brings to Ruby users. My Ruby/Tk Tutorial has a program p075hellotk1.rb that uses Ruby/Tk a graphical user interface. However, the documentation for Ruby/Tk is extremely poor and I would be comfortable in using Java Swing instead. The code in JRuby for the program p075hellotk1.rb can be something like this:

# javaSwingHello.rb
require 'java' # Line 2
JFrame = javax.swing.JFrame
JLabel = javax.swing.JLabel
frame  = JFrame.new
jlabel = JLabel.new("Hello World")
frame.add(jlabel)
frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)
frame.pack
frame.setVisible(true)

Run the above program from the command line as follows:

jruby javaSwingHello.rb

Line 2: The second line of the above program enables JRuby’s Java support and allows a Ruby program to use Java classes.

b. Calling JRuby from Java. JRuby can just as easily be called from Java, using either the JSR 223 Scripting for Java 6 or the Apache Bean Scripting framework. More information on this is available in the JRuby Wiki

c. Running Rails with JRuby. The advantages are obvious. To quote the JRuby Wiki:

  • Gives Rails the power and functionality of Java: Virtual Machine, application servers, and libraries.
  • With future JVM and JRuby improvements, JRuby may be faster than Matz’ Ruby Interpreter in running Rails
  • Gets Rails in the door of Java shops by making Rails apps into Java-platform apps.

To conclude, I am surely liking JRuby and would definitely explore it further. Ignore JRuby at your own peril !

Resources:

Go on! Digg It !

Technorati Tags: , , ,

Posted by Satish Talim




RUBYGALORE.COM