Keeping it Small and Simple

2007.05.28

Yes, JRuby is Ruby

Filed under: JRuby, Ruby Programming — Lorenzo E. Danielsson @ 19:52

JRuby is an implementation of the Ruby language. Contrary to what some people seem to think, it is not a stripped-down version of Ruby (look elsewhere for that). And it is not a different language. (Somebody actually tried suggesting to me the other day that the relationship Ruby-JRuby is comparable to that of Java-JavaScript. That is utter nonsense.)

So JRuby is Ruby, with the added bonus of giving you access to the Java class library. And no, that does not mean that you have to write Ruby code that looks like it was Java. Let’s take a look.

Looking inwards

You can learn a lot about JRuby classes by reflection. Here, for example, I create an instance of a javax::swing::JLabel and then list all the available methods.

1 #! /usr/local/bin/jruby -w
2 
3 require 'java'
4 
5 label = javax::swing::JLabel.new "Test"
6 label.methods.each { |method|
7   puts method
8 }

If you run this program it will list all the methods available on javax::swing::JLabel instances, and there are many (./listmethods.rb | wc -l tells me 917 lines). You can always use grep to find specific methods. For instance, I wanted to find only methods that contain the word visible:


% ./listmethods.rb | egrep '[vV]isible'
compute_visible_rect
scrollRectToVisible
visible_rect
visible=
set_visible
setVisible
visibleRect
get_visible_rect
computeVisibleRect
scroll_rect_to_visible
getVisibleRect
visible
visible?
is_visible
isVisible

If you study this output you will notice that the JRuby “java classes” come packed with goodies to make things feel “right” for a Ruby programmer. For instance, instead of:


javax::swing::JFrame.new.setVisible(true)

you can, if you so wish, do:


javax::swing::JFrame.new.visible = true

Let’s look at an example where we create a JFrame holding a JLabel. But let’s make it look like Ruby, not Java (not that I’m suggesting there is anything wrong with Java).

 1 #! /usr/local/bin/jruby -w
 2 
 3 require 'java'
 4 
 5 include_class 'javax.swing.JLabel'
 6 include_class 'javax.swing.JFrame'
 7 
 8 class Frame < JFrame
 9   def initialize
10     super "JRuby/Swing Demo"
11     default_close_operation = JFrame::EXIT_ON_CLOSE
12     label = JLabel.new ""
13     label.text = "JRuby is Ruby!"
14     add label
15     pack
16   end
17 end
18 
19 frame = Frame.new
20 unless frame.visible?
21   puts "The frame exists but is not yet visible."
22 end
23 frame.visible = true
24 

If you look at lines 11, 13 and 23 you see that I am using assignment instead of setter methods. (Incidentally, it seems that default_close_operation is broken, at least for me. Then again, I am using a release candidate..) Also on line 20 I am using frame.visible? rather than frame.isVisible(). So if you already are a Ruby programmer, there is no need to learn “the Java way” (although that also works).

I’ll give you one final example. How would you create a singleton class in JRuby? The same you would do it in Ruby, of course. Look at the following:

 1 #! /usr/local/bin/jruby -w
 2 
 3 require 'java'
 4 require 'singleton'
 5 
 6 include_class 'javax.swing.JLabel'
 7 include_class 'javax.swing.JFrame'
 8 
 9 class SingletonFrame < JFrame
10   include Singleton
11 
12   def initialize
13     super "Singleton Frame"
14     setDefaultCloseOperation JFrame::EXIT_ON_CLOSE
15     label = JLabel.new "JRuby/Swing"
16     add label
17     pack
18   end
19 end
20 
21 frame1 = SingletonFrame.instance
22 frame2 = SingletonFrame.instance
23 
24 puts "frame2 visibility: #{frame2.isVisible}"
25 frame1.setVisible true
26 puts "frame2 visibility: #{frame2.isVisible}"
27 

Note that frame1 and frame2 are references to the same object. Also, you cannot call new on SingletonFrame (go ahead and try it). This works just like any Ruby programmer would expect.

I have just scraped the surface of what is possible with JRuby (after all this is not a tutorial). Hopefully I may have tickled somebody’s curiosity out there. And hopefully I’ve been able to show with these few examples that by using JRuby you are not “losing” anything as compared to Ruby (and then I mean C-Ruby or whatever you want to call it).

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: