Online coverage from the Sun Developer Network staff 2008 JavaOne Conference

Thursday May 08, 2008

Rick Palkovic,
Sun Microsystems Staff Writer

If you use one of the scripting languages JRuby or Groovy, you’re unlikely to use the other, and are probably curious about their relative advantages. Most of us don’t have broad expertise in both languages, so the session presented by Neal Ford, "Comparing JRuby and Groovy," TS-6050 provided valuable insights.

Ford characterized both languages as dynamic and strongly typed, but noted that they were created for different purposes. Groovy has been designed to provide a modern dynamic syntax for generating Java bytecode.  JRuby is a Java platform port of the Ruby language. Ruby already runs on all of the popular operating systems, and JRuby tries to be no more than Ruby running on the JVM.

Because of their different geneses, the two languages have different priorities. Groovy was designed to bring modern semantics to the Java bytecode, and JRuby was designed to bring the richness of the Ruby language to the Java platform.

Besides differences in syntax, Ford described many other differences between the two languages that affect the way programmers approach solving problems in each. For example, each language handles the concept of null differently. In the Groovy world, null is always null. Groovy adds a "?." operator to help avoid null pointer exceptions. For example, the expression

person?.address?.street

tests person and address for nullness, and will only execute the line of code if both person and address are not null.

In JRuby, nil is an instance of NilClass. Nil is an object because, in Ruby, everything is an object. And all objects have consistent semantics. No special operator is required to handle null or nil. Becuase nil is an object, you can call methods on it. Ford then presented the following lines of JRuby code, which define a blank? method and add it to the String class.

class String
  def blank?
     empty? || strip.empty?
  end
end

JRuby supports open classes, and when it looks on its class path, it sees that a String class is already there. The code reopens that String class and adds the blank? method to it -- in Ruby, the convention is to append the question mark character to the name of a method that returns true or false. With the following JRuby code, he then used the blank? method to create a test_blank method:

def test_blank
  assert "".blank?
  assert " ".blank?
  assert nil.to_s.blank?
  assert ! "x:.blank?
end

Here, the test_blank method operates on the String object itself, and if it is empty --  or if you strip all the spaces away and it is empty -- then the String instance must be blank.

Interestingly, if you test this code you can also assert that nil.to_s.blank? is blank. Nil is an object in Ruby, so you can call methods on it, including to_s (convert to String). And, as it turns out, nil.to_s is empty -- which is exactly what you would expect.

Ford presented many more intriguing examples of differences between the two languages that affect the way a programmer approaches the solution to a problem. It is worth a look at the transcript in Java online when it becomes available. Meanwhile, Neal Ford has made his presentation slides available on his website at nealford.com.

For more information on scripting languages, see the technical articles and tips on java.sun.com.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed