Introduction to Scala for Rubyists

This blog provides Rubyists with an easy introduction to the Scala programming language. It will show you how to write Ruby-like code in Scala. As you get more familiar with Scala, you can slowly transition from the Ruby way to the Scala way.

Learning a new language is psychologically painful. Learning Scala the Ruby way limits the pain of the transition — let’s dive in!

Variable assignment

Ruby is a dynamically typed language, so variables can be assigned to an object of one type, and then reassigned to an object of a different type.

Scala is statically typed so once a variable is assigned, it can only be reassigned to other variables of that type.

Scala also supports immutable references, which are variables that cannot be reassigned, even to other objects of the same type. The val keyword is used to make immutable references.

Scala variables with the Any type can be reassigned to values of any type and behave a lot like Ruby variables.

Experienced Scala programmers don’t use var and Any in this manner, but it’s fine when you’re just getting started!

Arrays

Ruby is dynamically typed, so arrays can contain different types of objects.

Scala has several types of Array-like data structures. We’ll only talk about the List data structure in this article. Other Scala guides would take this opportunity to go off on a huge tangent on the different types of arrays in Scala, but we’ll refrain — you’re welcome ;)

Scala is statically typed, so lists generally need to contain objects of the same type.

We can use the Any workaround again to create lists with different types of objects.

Array mutability

Ruby arrays are mutable, so elements can be added or removed.

Scala lists aren’t mutable, but you can create a new list by unioning two lists.

Scala has mutable array data structures, but we’re not going to dig into those right now. We’re avoiding the Scala rabbit holes.

Collection Methods

Ruby borrowed methods like map, filter, and inject from functional programming languages to make it easier to work with collections.

Ruby also provides symbol to proc syntactic sugar to allow for a more concise syntax.

Scala also provides a map method for lists.

Scala provides some syntactic sugar with the underscore character.

Classes

Typical Ruby classes use mutable instance variables to maintain state.

Let’s create a similar Scala class that is instantiated with variables and creates mutable objects.

Scala programmers prefer immutable objects, but it’s fine to write code like this when you’re getting started.

Singleton methods and companion objects

Ruby classes can define both singleton methods and instance methods.

Scala companion objects allow for behavior that’s similar to singleton methods.

The class Nintendo and object Nintendo definitions must be in the same file for the Scala compiler to interpret object Nintendo as a companion object. Otherwise, the compiler will interpret the object definition as a namespace collision.

Modules and Traits

Ruby uses modules to add instance methods to a class.

Traits in Scala are similar to modules in Ruby.

Nesting modules vs. packages

Ruby classes are organized in a nested module structure that mimics the directory structure.

For example, the lib/turf/lookup.rb file looks like this in the Turf open source project:

Scala classes are located in a nested package structure that follows the directory structure.

Here’s what the src/main/scala/com/github/mrpowers/spark/daria/sql/SparkSessionExt.scala file looks like in the spark-daria open source project.

Scala follows some crazy Java directory structure conventions, so code is often nested deeply inside empty directories. If you’re completely new to the Java / Scala ecosystem, the conventional directory structure will seem ridiculous. Thankfully Scala packages provide a clean way to navigate the maze of empty directories. Deeply nesting Ruby modules gets clunky and Scala’s package notation really shines in comparison.

Monkey patching

Ruby makes it easy to add methods to existing classes with a process called monkey patching.

Scala allows for monkey patching with implicit classes.

Next Steps

Jason Swartz wrote an awesome Scala book that’s approachable for Ruby developers.

I’ll write another blog post covering more advanced language features soon :)

Spark coder, live in Colombia / Brazil / US, love Scala / Python / Ruby, working on empowering Latinos and Latinas in tech