Scala: The best of both Ruby and Java

Many programmers love Ruby, they just can’t get enough of it. Its probably one of the most aggressively evangelized languages since Java first came on the scene. They generally cite features that include a flexible and extensible syntax, closures, and how concise and expressive the code can be.

For example, you can create a Map (Ruby calls them “hashes”, even though a hashtable is only one way a map can be implemented) with a simple syntax like:


numberMap = {"one" => 1, "two" => 2, "three" => 3}


The Java equivalent of this is far more verbose:


Map<String, Integer> numberMap = new HashMap<String, Integer>();
numberMap.put("one", 1);
numberMap.put("two", 2);
numberMap.put("three", 3);


The Java version does have one advantage though, which is that we’ve told the compiler that numberMap uses Strings for keys, and Integers for values. If we try something like:


numberMap.put(4, "four");


The compiler will complain, or if you are using an IDE like Eclipse, you will be told there is a problem almost the moment you type this. This is called “static typing”, or “compile-time type checking”. Ruby’s approach is called “dynamic typing” or “run-time type checking”.

In Java you must painstakingly tell it the type of each variable you use, and this is one of the main reasons Ruby is more concise and easier to read than the equivalent Java.

Java has another advantage, which is that it is far faster than Ruby. In some benchmarks Ruby requires over 15 times as long to perform a CPU-intensive task as Java (and this has caused real life problems for some Ruby users).

So where does Scala come in? Lets look at the map example in Scala:


var numberMap = Map("one" -> 1, "two" -> 2, "three" -> 3)

You will notice that it looks very similar to the equivalent code in Ruby, but there are some important differences. In particular, just like Java, the Scala compiler knows that numberMap uses Strings as keys, and Integers as values. Unlike Java, you didn’t have to tell it this, it just figured it out for itself! This is called “type inference”.

This means that if I try adding a new key-value pair to numberMap, but using an Integer for the key, and a String for the value, Scala will complain as soon as you try to compile it (or your IDE will warn you immediately). With Ruby, it is only likely to cause a problem when you run your software and try to retrieve this key and value from the Map and get an Integer and a String, rather than the String and the Integer that you were expecting.

It is hard to overemphasize what a timesaver compile-time type checking can be, it eliminates a whole class of bugs that would otherwise occur at runtime, and Scala brings you the benefits of this, without the verbosity.

Scala also has a bunch of other nice Ruby features, which Java lacks, including closures, and a very malleable syntax that makes it well suited for “domain specific languages”. It has all of these features, and combines them with the benefits of static typing.

But there is more. When you compile some Scala code, it is compiled to Java class files, and can be run on a Java Virtual Machine just as if it was compiled Java code. This means that Scala can go wherever Java can go, it can also use any of the vast number of libraries that have been built for Java, and it can take advantage of today’s very efficient Java runtime environments. Because of this, compiled Scala is almost as fast as compiled Java!

Scala is still quite young. Its own libraries are impressive (especially the scala.actors library), but I’ve found one or two bugs already (in the JSON parser lib), suggesting that its still somewhat immature.

Nonetheless, for anyone yearning for the advantages of both Ruby and Java in one language, you should definitely take a look!

3 thoughts on “Scala: The best of both Ruby and Java

  1. Pingback: What can you do to add value to yourself if you don't graduate from a top-tier college? - Quora

  2. Pingback: Is Scala the next big thing? | Ask & Answers

  3. Pingback: Is Scala the next big thing? | Technology & Programming Answers

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>