Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving From non-Java Ruby to Groovy: Language Differences

Tags:

java

ruby

groovy

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....

like image 714
Dan Rosenstark Avatar asked Dec 29 '22 02:12

Dan Rosenstark


2 Answers

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.

like image 157
noah Avatar answered Jan 13 '23 21:01

noah


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.

like image 42
Shadowfirebird Avatar answered Jan 13 '23 21:01

Shadowfirebird