The recent hype around Rails has led to the Ruby language shooting into prominence. Satish Talim who has been working with Java for over a decade, has also been trying out Ruby.
Based on his experience, he has now
put down free study notes on the Ruby language, titled Come Learn Ruby with Satish Talim. Here’s an excerpt from the section about Ruby for Java developers.
Similarities between Java and Ruby
As with Java, in Ruby…
* memory is managed for you via a garbage collector.
* there’s public, private, and protected methods.
*
you’ve got embedded doc tools (Ruby’s is called RDoc). The docs
generated by rdoc look very similar to those generated by javadoc. RDoc
can produce fairly good content even if the source contains no comments.
Differences between Java and Ruby
Unlike Java, in Ruby…
* you don’t need to compile your code. You just run it directly.
* there’s different GUI toolkits. Ruby users can try WxRuby, FXRuby, or the bundled-in Ruby Tk for example.
* you use the end keyword after defining things like classes, instead of having to put braces around blocks of code.
* you have require instead of import.
* all member variables are private. From the outside, you access everything via methods.
* parentheses in method calls are usually optional and often omitted.
*
everything is an object, including numbers like 2 and 3.14159. Classes
are objects! For example, Array is a constant name that is bound to the
Array class object. To create a new object, we call new on the class
object as in a = Array.new
* there are no primitives or data types
* variable names are just labels (not objects). They don’t have a type associated with them.
* there’s
no type declarations. You just assign to new variable names as-needed
and they just “spring up” (i.e. a = [1,2,3] rather than int[] a =
{1,2,3};).
* it’s foo = Foo.new(“hi”) instead of foo = new Foo( “hi” ).
* the constructor is always named initialize instead of the name of the class.
* you have “mixin’s” instead of interfaces.
* YAML tends to be favoured over XML.
* it’s nil instead of null. Also, nil is a normal object; you can never get a null pointer error!
* there is no method overloading.
* it’s much more common to put many classes in the same file.
Do check out the entire guide to Ruby.
If you have used both Ruby and Java, please add your comments below.
Related
Sun Embraces Ruby
Let Java retire from the spotlight of web applications in dignity
Groovy bridges the scripting and the enterprise Java worlds
