Sbrady, Software blog

home

Groovy vs Ruby vs Java

11 Jul 2014

I had spent a good deal of time working with Java, and some point around 2007 I became interesting in Ruby, and started development Rails apps. I really enjoy the dynamic nature of Ruby. However I have come to miss the expressiveness of static typing. Recently I did a little work on a Grails app and found that Groovy seemed to be doing everything I had ever wanted. So I put together his language feature fight card below. As you can see Groovy comes up on top for my favourite language features.

Groovy Ruby Java
Classical inheritance
class Person {}  
class Engineer extends Person {}
      
class Person end  
class Engineer < Person
    
class Person {}  
class Engineer extends Person {}
    
Interfaces
interface Person { void talk(name) }  
class Engineer implements Person{
  def talk(words) {
    println(words)
  }
}
//implement interface with map
def person = [
  talk: {words -> println words}
] as Person
      
DERP..
    
interface Person 
{ 
  void talk(String name); 
}  
class Engineer implements Person{
  public void talk(String words) {
    System.out.println(words);
  }
}
    
Mixin
class Talk {
  def talk(word){
    println word
  }
}
@Mixin(Talk)
class Person {}
module Talk
  def talk(word)
    puts word
  end
end
class Person 
 include Talk
end  
    
DERP.
    
Name Spaces
package io.sbrady.people
@Mixin(Talk)
class Person {}
DERP. module namespaces  
    
package io.sbrady.people
    
Dynamic Typing
def talk(word){
  println word
}
def talk(word)
  puts word
end
    
DERP.
    
Static Typing
def talk(String word){
  println word
}
DERP.
    
public void talk(String word) {
System.out.println(word);
}
    
Lambda/Closures
static talk(talkFoodClosure) {
  talkFoodClosure("hi there");
}
static talkFood {
  def food = "pizza"
  talk({greeting->
    println "$greeting eat $food"
  })
}
def talk(talkFoodlambda)
  talkFoodlambda.call("hi there")
end
food = "pizza"
talk(lambda{|greeting| 
  puts "#{greeting} eat #{food}"
})
    
public static void talk(
  Consumer<String> talkFoodlambda)
{
  talkFoodlambda.accept("hi there");
}    
String food = "pizza";
talk((greeting)-> { 
  System.out.println(
    greeting +" eat "+food
  );
});
    
Collections (oldest person)
people.max({person-> person.age})
people.max{|person|p.age }
    
people.stream()
  .max(
    (person1,person2) -> 
        (person1.getAge() 
           - person2.getAge())
   ).get();
   //better than it use to be..
    
Monkey patching (make person 21)
Person.metaClass.age = 21
class Person
  def age
    21
  end
end
    
DERP. DERP. AOP DERP.
REFLECTION DERP.
DO YOU REALLY WANT TO KNOW
    
Named Arguments
def add(params) {params.x+params.y}
add(x:100, y:99)
def add(x:0, y:0)
  x+y
end
add(x:100, y:99)
    
DERP. DERP. BuilderPattern
DERP.
    
Score
10/10
7/10
    
6/10
    
comments powered by Disqus