Is Scala the new Ruby?
A Ruby fan’s opinion.
Scala is a programming language that is getting a fast adoption into programming circles. It has the simplicity of Ruby and the robustness of Java, combined with full functional support and Object Oriented Programming. In my current experience (Less than 6 months with Scala at the time of this post) I see similarities everywhere between Scala and Ruby, but in many cases Scala’s syntax looks cleaner to me. Let’s take Ruby’s lambda expressions for example:
foo = ->(a,b){ a + b }
So here’s the catch. We take a variable called foo, and assign a function to it that receives two parameters (a and b) and returns the sum of this two parameters. Simple enough, but what about the same code in Scala?
val foo: (Int, Int) => Int = _+_
Note that Scala is a typed language but has Type Inference so you don’t need to specify the value type of foo, but you must specify a and b’s type. This way looks cleaner to me. I thing the key is the arrow thing that we find in both examples. Ruby decides to use it in front of the expression and then opens braces to define the function’s body. Scala’s approach is more direct: The arrow defines the frontier between the definition and the body of the function, so braces are only needed if your function has more than one line.
More Comparisons
So both languages are similar in many things, but of course the typed nature of Scala introduces some variations into play. Let’s see how the two languages compare when using some of the most common operations. Let’s take map for example.
array_two = array_one.map { |item| item + 1 }
val arrayTwo = arrayOne.map(_ + 1)
array = [0, 1, 2, 3]
val array = List(0, 1, 2, 3)
Now let’s look at class definitions:
class MyClass
def initialize(parameter1, parameter2)
# Constructor code here
end
end
class MyClass (parameter1:Int, parameter2:Int) {
# Class definition and constructor are the same.
}
We can even go a bit further and declare accessors directly on the class definition too. This is how it is done:
case class MyClass(var accessor1) {
}
In Ruby we need to use attr_accessor inside the class. So scala code is shorter here.
Every aspect of Scala looks more concise to me. I love Ruby, but sometimes the code gets a bit messy, and the non-typed and mutable nature of it makes the code get confusing at some point of the development process.
Gems
One of Ruby’s highlights is the ability to include gems into your project. Gems are external libraries of code. In scala we hava a similar system. Instead of RubyGems, we can use SBT. SBT allows us to include different external modules by defining a build.sbt file. This is very similar to ruby’s Gemfile. Here’s an example of build.sbt.
libraryDependencies += “com.gistlabs” % “mechanize” % “0.11.0" On the other hand, the availability of ruby gems is highly superior to the amount of SBT dependencies (For now…).
Rails vs Play2
OK, so we have compared some aspects of the two languages. But what about Frameworks?
Play2 is a framework clearly inspired by Rails, but it makes some things different. In Ruby, every controller is a class, whether in Play, every controller is an Object. Scala’s objects are like singleton classes (Or a class with only static methods in it).
Let’s suppose that we want to print JSON in our controller. In Ruby we would write the following:
def index
render :json => {:message => “Hello world”}
end
The same code in Scala looks like this:
def index = Json(“{message: ‘Hello world’}”)
Deployment
Both frameworks can be deployed using Heroku The process is the same, and combined with the power of SBT, it’s really easy to include “gems” in your project like you can do with Rails.
Still, I found easier to configure a working Rails environment than a Play2 environment. In the end it’s only a matter of taste. I still use Ruby and I think I will always be using it, but Scala provides an excellent alternative for more demanding apps . It clearly beats Java in every aspect, so it could be the next prefered enterprise platform.
Fore further reading, I recommend the book by Martin Odersky (Scala’s creator himself). The book is called: Programming in Scala Second Edition.
Email me when Federico Poli publishes or recommends stories