Java is mature. It’s tested. And it’s fast (contrary to what theanti-Java crowd may still claim). It’s also quite verbose. Going fromJava to Ruby, expect your code size to shrink down considerably. You canalso expect it to take less time to knock together quick prototypes.
Similarities
As with Java, in Ruby,…
- Memory is managed for you via a garbage collector.
- Objects are strongly typed.
- There are public, private, and protected methods.
- There are embedded doc tools (Ruby’s is called RDoc). The docsgenerated by rdoc look very similar to those generated by javadoc.
Differences
Unlike Java, in Ruby,…
- You don’t need to compile your code. You just run it directly.
- There are several different popular third-party GUI toolkits. Rubyusers can tryWxRuby,FXRuby,Ruby-GNOME2,Qt, orRuby Tk for example.
- You use the
end keyword after defining things like classes, insteadof having to put braces around blocks of code. - You have
require instead ofimport. - All member variables are private. From the outside, you accesseverything via methods.
- Parentheses in method calls are usually optional and often omitted.
- Everything is an object, including numbers like 2 and 3.14159.
- There’s no static type checking.
- Variable names are just labels. They don’t have a type associated withthem.
- There are no type declarations. You just assign to new variable namesas-needed and they just “spring up” (i.e.
a = [1,2,3] rather thanint[] a = {1,2,3};). - There’s no casting. Just call the methods. Your unit tests should tellyou before you even run the code if you’re going to see an exception.
- It’s
foo = Foo.new("hi") instead ofFoo foo = new Foo("hi"). - The constructor is always named “initialize” instead of the name ofthe class.
- You have “mixins” instead of interfaces.
- YAML tends to be favored over XML.
- It’s
nil instead ofnull. == andequals() are handled differently in Ruby. Use== when youwant to test equivalence in Ruby (equals() in Java). Useequal?()when you want to know if two objects are the same (== in Java).