Syntactic Sugar in Groovy and Ruby

2008/05/27

Over on the RailsEnvy blog (the guys who made the “Rails vs. X commercials” last year), there’s an article that highlights some of the nice syntactic sugar that Groovy has that’s currently not available in Ruby.

I actually came to Groovy/Grails after working with Ruby/Rails for about a year. Both languages and framworks have their strengths and it’s nice to see cross-pollination of good ideas going both ways. From the comments, it sounds like Ruby 1.9 is getting some of the features that we’ve had in Groovy for a while.

One thing that I’ve missed from Ruby is the strong category support (called Mixins in Ruby). I’ve been reading that the 1.6 version of Groovy is much stronger in this area, and I’m looking forward to trying it out to see how it matches up.

I used to think that the regular expression support in Groovy was lacking compared to Ruby, but after a little digging, I found that it’s just about as easy and powerful as Ruby is.

A couple of other things that I miss from Ruby:

  • package management through RubyGems (yes, we can use maven/ivy/etc, but it’s really no where as easy as having this at your fingertips:
    sudo gem install foo
  • multiple assignment of variables on the same line (though this will be available in groovy 1.6)
    a, b = "foo", "bar"
  • block regular expressions using %r{...} – these make it much easier to document really long regular expressions rather than one huge string
  • array.first and array.last to safely retrieve the first or last element out of an array.

    Yes, I can use ?.getAt, but it’s not as descriptive

    def array = ["foo", "bar", "baz"]
    array?.getAt(0)   // returns "foo"
    array?.getAt(-1) // returns "baz"
  • Being able to use ||= to instantiate a variable if it doesn’t exist and set it to a default value if it doesn’t already have a value (though the Groovy “Elvis” operator gets pretty close).

    This example instantiates val if it doesn’t exist and gives it the default value of “start_val” and then appends “foobar”, if val does exist, it will simply append “foobar” to it.

        (val ||= ["start_val"]) << "foobar"
  • Having the .irbrc file auto load ruby code when going into irb (the interactive ruby shell. Having a .groovyrc file that gets loaded when going into groovysh or groovyConsole would be great to attach custom enhancements to the Groovy language.

I know that some of this stuff can be added relatively easily through MOP, but it’s nice to have it right at your fingertips when you fire up a groovyConsole to try something out. The Groovy core team has been good about continuing to implement things from other languages that are useful and make sense for Groovy and overall, I’ve been a happy programmer for the last year that I’ve been using Groovy and Grails.

There are 4 comments in this article:

  1. 2008/05/29Dierk König say:

    Yes, Gregg attended my “Groovy in a day” event at DLW. It was great to have him there. I commented on his blog to clarify some of the misconceptions in the other comments. –Dierk

  2. 2008/05/29Hamlet D'Arcy say:

    A recent version of Groovy (1.5.4?) added head() and tail() methods to java.util.List, which I find quite useful. On an empty list it does throw a NoSuchElementException though. first() and last() also exist in list, but I’m not sure how recent those are. I’m looking at the svn repo (org.codehaus.groovy.runtime.DefaultGroovyMethods.java)

    After trying to fight w/ Ivy and Maven the last few days, I definitely feel the same envy for Ruby’s gem system.

  3. 2008/05/29Paul King say:

    These are available in 1.6:

    [a, b] = ["foo", "bar"]
    assert a == “foo”

    def array = ["foo", "bar", "baz"]
    assert array[0] == “foo”
    assert array.first() == “foo”
    assert array.head() == “foo”
    assert array[-1] == “baz”
    assert array.last() == “baz”

    Experimentally in 1.6 there is also a grapes equivalent to gems (not finalised yet) and an experimental block regular expression syntax (not finalised yet).

  4. 2008/05/30tednaleid say:

    Awesome! Thanks for the posts guys. It’s great to hear that Groovy is taking even more of the useful features from Ruby than I realized.

    Sounds like just about all of the main things that I’ve been missing from Ruby are coming over (or are at least being considered).

    About the biggest thing left, would be some sort of .irbrc equivalent for the groovy shell, and I think I might be able to hack something like that together pretty quickly (and contribute it back if people on the groovy mailing list think it’s useful).

Write a comment: