Saturday, March 2, 2013

Ruby vs Java

There are probably hundreds if not thousands of articles with the title "Ruby vs Java" or "Java vs Ruby"; nevertheless I decided to write down my considerations about the way these two languages compare to each other.
So let's start by looking at the advantages that each language has over the other.

Ruby advantages

Everything is an object

In Ruby everything is an object, even primitive types and classes. The fact that a class is an object makes it much easier to write unit tests because it allows you to mock them and to create stubs for class methods as well.
In Java, instead, you cannot mock static methods. This makes it difficult to test units that make use of them. You end up implementing instance methods even for functionalities that do not use any information about the object's state but just to facilitate testing.


Modules allow for a class to include functionalities without the need to extend any parent class. This makes it easy to reuse cross-cutting concerns like logging, security, transaction management and so on.
To achieve the same in Java you need to use delegation which can require a lot of repetitive code.
Module are sometimes abused, though, like Ryan Bates points out in this gist.

Blocks of code

This is one of the main attractions for newcomers to Ruby. Blocks of code allow for a very concise syntax to implement the Template Method Pattern.
Java requires the definition of an interface and multiple classes implementing it, this again results in more verbosity.

Duck typing and syntactic sugars

Both features make the code extremely compact and readable: no need for type definition or type casting, no semicolons at the end of the line, no parentheses for method invocations, operators overload, getters/setters automatically available for all instance variables and many, many more.
Ruby code is incredibly expressive and easy to read, many say indeed that "Ruby speaks English". To understand the concept it is enough to read some tests written using RSpec, which I consider one of the best library I ever used.
On the other hand, Java code is verbose and it can require multiple lines of code for stuff which you could get done in a single line in Ruby.

No compilation required

The thing that annoys me most while working on a Java web project is that every time I change something , even if it is just a Javascript or CSS file, I need to re-compile and re-deploy. This is not the case with Ruby resulting in a boost in productivity.

Java advantages


Performance can be the single reason to prefer Java over Ruby.
In web applications, though, 90% of the time is spent in the database, in requests to external systems and not in the language itself. If your web app is slow and you decide to replace Ruby with Java, you will probably not achieve much improvement.
On the other hand, if you are working on an application making intensive calculations, than Java outperforms Ruby. The following benchmarks executed on a x64 Intel quad-core running on Ubuntu gives you an idea of the difference in performance between the two languages:

Multi-threading and concurrency

The results from the previous benchmark derive also from the fact that Ruby, or at least the MRI implementation, does not support threads. JRuby and Rubinius do support threads but I think 95+% of Ruby production environments are based on MRI.
In a web application, it is necessary to launch different processes in order to be able to handle multiple requests which is something that does not scale very well.
Doing parallel computations is very problematic without threads. If using separate processes, the only way to let them share data is through a database, a file or some other external resource whose access speed is obviously way slower than memory.


Interfaces are a very simple and clear way to define the contract between a piece of software and the external world. Unfortunately, many people abuse them and create interfaces for every single class they write.
However, when used properly they do help understanding how a module works and how to use it. Furthermore, interfaces allow to modify a module or to completely replace it in a manner completely transparent to rest of the system.

IDE support

Eclipse, Intellij, Netbeans all make the life of every Java Developer so much easier by refactoring variables, methods, classes. They also let you quickly navigate through the classes and save a lot of keystrokes with auto-completion.
The features I consider the most useful are those for reverse engineering, like showing class hierarchies or methods usage. This can be a life-safer, especially when working on big, old projects, where many contributors were not writing really clean code, just to use an euphemism :)
Because of the extremely dynamic nature of Ruby, IDE cannot help much in this sense and without an extensive test suite it can be really hard to extend or modify an old Ruby project.


I think both Ruby and Java are great. It would be nice if they could somehow blend into one language that combines the best of both worlds. Actually, I think this language already exists!!! I am talking about Scala, of course.
Scala combines the flexibility, expressiveness and conciseness of Ruby with the performance, scalability and tools support of Java. Scala takes this even further by being a truly functional language which makes it suitable for concurrent, scalable and fault-tolerant applications.
Companies like Twitter and Linkedin are successfully using it and I believe there prospects a really bright future for this language.


  1. There is also JRuby, you can write lots of stuff in ruby and integrate already existing codebase in java or just write in java code that needs to be optimized :)

  2. Yes, I indeed mentioned JRuby as well :)

  3. What about java's built in GUI, and the ease of deployment?
    Is there anyway in Ruby to easily deploy an application with a GUI, without a lot of messing around installing say tcl/tk, then the ruby interpreter, then your code?
    I'd say that's a win for java in terms of useable code you might want to sell to someone. Run one jar file, as averse to lots of instructions on downloading this and downloading that.

    1. As for the gui part Rubymine is awesome, and i think you can compile ruby code with rubinius

  4. Nice post! Have you ever wondered how the transition between on to the other language works? My friend switched from Java to Ruby some time ago (she has described the move at; and it looks like it's not an easy ride.

  5. thats why I prefer groovy. its uses the JVM and its a very dynamic language.

  6. I think Groovy is the language you are looking for Scala for me seems to have to many options choice and incompatibilities. Should have followed the KISS principle.

    groovy on the other hand has nearly every feature you could desire is inspired by Ruby, Smalltalk and Python.

  7. thanks for the comparison...

  8. and what would be your opinion on Perl VS Ruby?

  9. Hi! I'm a batch programmer, or kind of. I'm not the classic programmer that makes useless batches, I make Folder-Encrypter (but with a loooot of security) or a programmable shut down program,etc...

    But I want to know another language. Ruby seems perfect! What do you think? Is it a useful language, unlike batch?

    I know Batch, Html and in future, Fortran. Well, and I can understand a bit C++. What do you think guys??

    - Renk

  10. Our Expert Developers are ready to take your projects. Send your project details & Get A Quote Now! Just Fill Out The Form Below. we are best expert in ruby on rails developer, ruby developer and rails developer

  11. I probably refer ROR. I am looking for Ruby on Rails Development in India for a very long time, so I came across Cryptex Technologies.


  12. Excellent Post, I welcome your interest about to post blogs. It will help many of them to update their skills in their interesting field.
    ccna training institute in Chennai|ccna institutes in Chennai

  13. Thanks for this blog post. I'm currently learning Java and it really helps comparing it to a familiar language.

  14. Really interesting article presenting what's best about each language. Thanks ;) and props for mentioning Scala!