Scala and Ruby Rosetta Stone August 17, 2011

I write a lot of Ruby at work and Scala in my spare time. I thought others who know one language but not the other might find a rosetta stone for the two languages useful.

For a more thorough rosetta stone for a variety of languages check out Rosetta Code.

The scope of each example is short so that it is easier to jump from one language to the other to compare specifics. There are a lot more things you can do in each of these categories in both languages. However, if there are other examples you think should be added then fork the gist in question and let me know about it in the comments.


Here are a few examples of how you might define some basic classes in each language. You can see here that Scala manages to be a bit more concise than Ruby.




Both Ruby and Scala support the idea of Mixins. They are known as modules and traits in Ruby and Scala respectively.

The examples below are mixins that specialize the results of an existing method on the target class. Classic examples of this are Enumerable in Ruby, and Seq in Scala. Each of these depend on one or more methods in their target class. Since Scala is statically typed the trait must specify something about the class it will be mixed in to. You can do this with both nominal (named) types and structural types (similar to duck typing). I show both of these in the Scala example below.

Lastly, the Ruby example depends on in_groups_of which comes from ActiveSupport (part of Rails).

Ruby modules

Scala traits


Below are a few of the things you can do with collections in each language. Scala has a bit more flexibility here when it comes to allowing you to pick the kind of performance characteristics you want. However, when in doubt, just use an IndexedSeq.

Scala also provides mutable and immutable versions of their collections. Ruby collections are mutable but that doesn't mean that each of the methods on a Ruby array will mutate the array. Enumerable#map, for instance will return a new array. The convention in Ruby is that if a method ends in ! then it will mutate the object and otherwise it will not. This convention is mostly observed in the core and standard libraries, but not entirely. Array#delete, for instance, is a Ruby core library method that mutates an object but does not end with a !.

Ruby collections

Scala collections

Hash maps

Each languages' core library comes with a hash map implementation. Ruby calls theirs Hash and Scala theirs Map. Great.

Ruby has hash literals which is convenient. Scala's flexible syntax allows a really close approximation of hash literals, syntactically, without the addition of specialized syntax for them.

Ruby hashes

Scala maps

Case statements and pattern matching

I don't care much for mentioning case statements in Ruby on their own. They are, however, the closest corollary to pattern matching in Scala. Scala's pattern matching support is very powerful. I'll present how you would accomplish some similar tasks in each and then show a few more powerful pattern matching examples in Scala.

Ruby case statements

Scala pattern matching

blog comments powered by Disqus