Monday, February 18, 2008

To Ruby or To Scala?

I have been exploring alternate languages for JVM for some time now.
Why? Certainly not because I think the Java language is dying. Though java is devolving, it has crossed the threshold of immortality - there are way too many applications written in java and java developers can be busy just by maintaining them (and also by inventing 'frameworks' to get around the deficiency of the language like DI, AOP etc). However, I have started to realize the power of languages - maybe even to the extend of believing the Sapir–Whorf hypothesis. And finally, I still believe that the JVM is one of the best deployment platforms available today and java has lots of ready to use libraries - so I don't want to miss them as well. Luckily, there are host of new languages targeted at the JVM - Jython, JRuby, Groovy, Scala, Rhino, Clojure etc.

Finally I've narrowed down to two: JRuby and Scala. I'll try to outline the positives and negatives of both.

Ruby is an extremely dynamic language. It is dynamically (and strongly) typed. In my opinion, Ruby is the best language for internal DSLs (maybe next only to Lisp). Most of the successful Ruby projects have some kind of DSLs - like Rails, Rake, Builders etc. In a way, DSLs are nothing but flexible and easy-to-use interfaces.
Scala is a statically (and strongly) typed language. However, it has a powerful type inference system to avoid the pain of providing the types. It looks suitable for large systems, where precisely defined interfaces are required.

So looks to me that these languages have their own niche in programming. If you need RAD, clean, flexible and easy-to-use interfaces and small to medium sized applications, use Ruby. For large scale applications, where type safety is more important, use Scala without the burden of statically typed language.

Lets see how these fair in the popularity factor:
1. Popularity: Ruby is already a popular language (thanks mainly to Rails), while Scala is not (yet).
2. Ease-of-learning: Scala is probably a bit easier for Java folks. But in terms of documentation Ruby clearly excels. Scala documentation is poor, though it is improving.
3. Ease-of-writing: Ruby maybe an inch ahead as it is dynamically typed.
4. Ease-of-reading: For large systems, Scala seems to be a winner (thanks to static typing).
5. Terse: Both languages support functional/OO/imperative style, and not much difference here as well.

I would also outline some cool features of these languages:
Mixins: Both of these languages support mixins (though in Scala it is in a controlled way)
Open classes: Basically you can add methods to existing classes any time you wish. Scala also supports this through implicits.
Functional programming: Both languages has true closures, you can use functions as values etc.
Again, in Ruby you have complete freedom, while in Scala it is more controlled.

Of course, as a last point, the tool support is no way near to that in Java. The IDE support (in terms of auto completion, debugging, profiling etc) is currently mediocre in these languages. Also the performance is not that great. It could only be a matter of time, though. And btw, we needed those IDEs because Java was too bad anyway :)

To conclude, there is no silver bullet in programming languages. I guess we are moving towards polyglot programming where synergy of multiple languages will greatly help in tackling complexity.


Anonymous said...

I would *highly* recommend Scala over Ruby for a large-scale system. Ruby is great in its way, but Scala is just far easier to work with on any sort of large-scale project. It's ironic, but the very dynamic freedom that makes Ruby so phenomenal also makes it difficult to maintain when the project grows beyond a couple thousand LOC. Using a nice tool (like NetBeans Ruby) of course mitigates this, but the problems with dynamicness still remain.

I haven't had that much experience with Scala on really large projects, but I can tell you from my experience on smaller ones that it's very nice and much easier to maintain than Ruby, primarily because it is much stricter in what it enforces. That language rigidity is what helps keep maintainability high and avoid "mystery code".

paulk said...

Most of my customers prefer Groovy over Scala (most of them still give me blank stares when I mention Scala) and Ruby (which the early adopters liked but there is now push back). Most of them do come from a Java heritage so have biases to Java-like syntax and close Java integration.

mykol said...

i'm going with Groovy. for me, it has the best JVM integration and has most of the dynamic/scripting language goodies. documentation-wise, it's got pretty good coverage. as for IDE, it's on IntelliJ, need i say more? :)

bodhi said...

First of all Groovy does not compare against Scala, as both of them are for different purposes.
So the real question is Groovy or Ruby. I chose Ruby for the following reasons:
1. Beautiful code. This is my opinion, but many people have also 'felt' that there is some inherent beauty in Ruby code.
2. Support/Documentation. There is no doubt that Ruby has a much better community and documentation
3. Proven. Ruby is already proven outside the JVM. It only remains to be seen how robust the JRuby implementation is.

Kit said...

Regarding performance, there are some benchmarks (eg. here that show Scala outperforming plain Java.

Ze'ev said...

If you want a Java-like dynamic language then I think Groovy is your choice. If Java-likeness is not a requirement then I think you should consider Jython. I find Python's syntax much more readable and easier to learn then Ruby's.

Daniel Berger said...

So, Daniel, you haven't had much experience with Scala on really large scale projects (or any, I'll wager), but you highly recommend Scala over Ruby for a large scale project, eh? Ah, the warm, fuzzy feeling of rigidity!

Too funny.

Anonymous said...

Although I haven't used Ruby myself in any project, I also recommend Scala over it. Mostly for static typing and performance! (I used to code in PHP before I moved to Java, which drove me to prefer static typing :))

Scala performance is mostly on par with Java. In some areas it can perform better: the List data type has excellent performance in "deeply" recursive functions, and XML handling in Scala can easily outperform Java (or run exactly as fast while making code several times shorter and hell of a lot more readable). Let me put it this way: in a recent test I did with a colleague, a Ruby script handling XML ran a couple of hours (couple of mins with hpricot), while the equivalent scala code ran for about 4 seconds *.

Also, Scala code can be even more concise than Ruby sometimes.

* PS. the shortest & most readable equivalent Java code ran for 2 minutes.

Anonymous said...

As an aside, I would recommend *against* using Jython unless you have a lot of Python experts in your development team or perhaps some legacy Python code. Jython just isn't to the point where it's anywhere near as stable and reliable as JRuby or even Groovy. They're getting there (I'm certainly not knocking their efforts), but I don't think Jython is ready for production just yet.

Mark Thomas said...

I decided to learn JRuby because it is available both on the JVM and off. I am currently building a large-scale system and "dynamic freedom" doesn't get in my way. As long as you follow good practices (prefer subclassing over monkey-patching, take advantage of Modules to prevent class name collisions, etc) then the code stays maintainable.

I don't think maintainability is a function of language strictness.

Vladimir Kelman said...

"But in terms of documentation Ruby clearly excels. Scala documentation is poor, though it is improving"

I'm currently reading an excellent "Programming in Scala" book by Martin Odersky:

"The IDE support (in terms of auto completion, debugging, profiling etc) is currently mediocre in these languages."

That's definitely true for Scala. However, there are three Scala plugins, which are [actively?] developed right now: for Eclipse, for Netbeans, and for IntelliJ IDEA. All of them are still betas, but for the last one there is at least a very good support (forum) available:

bodhi said...


I wrote this blog entry before the book came out ;). Anyway, the book is not free. I believe the amount of quality documentation that is freely available also matters. Consider java or ruby.

pstickne said...

If you can stand reading through a "semi-formal" specification, Scala, the language, is pretty well documented -- and freely available on (This might not help the beginning programmer, but for anyone who is already waist-deep in the water, it's a great place to start.)

That being said, it will be nice when a wider range of books covering Scala are published (maybe some "free"?) and when the Scala library itself is more documented (volunteers welcome!).

Still, for a "new" language (Prior to Scala 2.x+ it was hiding in Academic-land with little/no external use or visibility) that has much better OO and type system than Java and makes FP constructs rather accessible, yet fits the same "strong/static" area as Java and works seamlessly** on the JVM, it's definitely something to look at. (And maybe less scary than CML or F# :-)

All I have to say about static/dynamic typing: don't judge between the two based on experiences with "types" in less-expressive languages (C, C#, Java, etc.). Scala (and other languages that take expressive typing seriously such as Haskell and Ocaml, etc.) are completely different and, at least for me, "make it work". Now I can't stand using Python or Ruby for anything bigger than a few pages ;-)

For me, the power of static typing (using a GOOD type system) comes with being able to clearly express the type I am dealing with and access it in a type-safe way. Scala also supports structural typing ("if it walks like a duck and talks like a duck...") in a type-safe way and it's possible to rather simply invoke methods dynamically. Now, method_missing / __get_attr__ / AUTOLOAD ... well, that magic exists elsewhere; but many cases are taken care of nicely with Implicits.

Scala is also rather DSL-friendly. Okay, so it's no Lisp-2 macros, but it's also no Java. The (standard) parser-combinator library is just one example, while Actors another (to a lesser extent), and then there is Lift and all the niceties it adds, being a nifty "web framework" and all. The Scala syntax is rather powerful.

FYI: Classes in Scala have types, but not all types are classes. This is a *huge* difference from Java and allows the type system to start shining.

**There are still a few minor corner cases, particularly with annotations (these should be resolved soon) and calling Scala from Java may require extra work (mangled names, etc.); however, interfacing with Java from Scala is a breeze -- it seems more natural than Java from Java (or, at least, much more enjoyable!)

And, because I just had to keep typing: Scala, like any self-respecting language, comes with a REPL and "interpreter". It works decently for scripting.

Anonymous said...

negative effects of viagra super viagra pro sample of viagra buy viagra online at what is generic viagra problems with viagra viagra over the counter pfizer viagra viagra from canada buy viagra in england viva viagra cheapest viagra in uk women does viagra work natural viagra substitutes

Vinny said...

Outstanding job ! Your web blog has given me most of the understanding I wanted .
Newark locksmith