Dan Rosenstark
Dan Rosenstark

Reputation: 69777

Moving From non-Java Ruby to Groovy: Language Differences

Using my not-outstanding Google skills, I have not been able to find a decent tutorial on Groovy for Ruby programmers. There are a lot of political pieces (Ruby is great! Groovy is great!) and tiny little contrasts, but I really don't care which is better. I know Ruby (and Java) relatively well, and I'd like to learn Groovy.

Would anybody care to (either provide an amazing link or) mark some differences between the two languages in terms of how to do things (syntactic, class declaration, loops, blocks, etc.)? For my purposes you can assume full Java competence to explain.

Again, I am not interested in knowing which is better. Just need to know how to do stuff....

Upvotes: 4

Views: 696

Answers (5)

Ken Liu
Ken Liu

Reputation: 22914

The Groovy wiki has a good article entitled Groovy style and language feature guidelines for Java developers.

Upvotes: 0

will
will

Reputation: 5071

We need more questions like this one. Three years after the question, there's still a comparitive lack of information on this moving betwen these two similar languages.

I did find this Slide Share presentation, which covers a lot of basic ground.

And this blog post was helpful with 'simple' stuff because it give a bit more background:

The reasons to switch between languages usually have more to do with project needs than a language itself and I feel its important able to swap and compare between tools.

One standard resource for this kind of question is: Rosetta Code.

Hope to see some more tips added to this list.

Cheers, Will

Upvotes: 2

Dónal
Dónal

Reputation: 187379

The differences between Java and Groovy are less than the differences between Ruby and Groovy, so if you know both Ruby and Java, it probably makes more sense to look for a "Groovy for Java programmers" book or tutorial.

IMO, the best Groovy book on the market is Programming Groovy. It's the most up-to-date book that I know of (though still a few versions behind the latest release), is pretty concise, and covers some relatively advanced topics in quite a bit of detail (e.g. meta object protocol).

Upvotes: 1

noah
noah

Reputation: 21519

If you know Java, the best thing you can read is how the metaClass is used in Groovy. Here's a decent explanation: http://skillsmatter.com/downloads/Groovy%20User%20Group%20December%202006.pdf

Just remember that everything in Groovy runs through the metaClass. The seemingly simple statements:

a = foo.bar
bar = b
foo.baz(1,2,3)

Translate roughly into this Java:

a = foo.getMetaClass().getProperty("bar");
this.getMetaClass().setProperty("bar",b);
foo.getMetaClass().invokeMethod("baz",new Object[] {1,2,3});

Everything is dispatched through the metaClass, which is how pretty much all the Groovy "language" features work. The most important feature is probably closures. What you need to remember about closures is that it's all metaClass trickery. The closure's metaClass can be setup to try invoking methods/resolve properties on its delegate, which basically means you can do things like invoke a method on an object that doesn't have that method.

Upvotes: 5

Shadowfirebird
Shadowfirebird

Reputation: 757

Did you see this and this? Relatively short posts, I know. You're right; there doesn't appear to be much...

update: two more links.

Upvotes: 2

Related Questions