Skip to main content

Ruby, Scala, and Complexity

Posted by cayhorstmann on March 2, 2011 at 9:05 PM PST

In this blog, I ponder why Ruby and Scala are easy to learn and complex to master, and how their cultures differ. ♦

The Lunar new year has passed, and a new semester has started at Ho Chi Minh City University of Technology, where I am spending my sabbatical . Last semester, I taught the standard undergraduate programming languages course, mostly using Scala. This semester, I teach a second course that is not required, so I can do any fun stuff I want. 

I am using Bruce Tate's excellent Seven Languages in Seven Weeks book for guidance (which I recommend highly). But I am deemphasizing syntax even more than Bruce does, and I diverge from his treatment by choosing some topics and languages that are more academic/cutting edge.

We started with Ruby, just as Bruce does. Why Ruby? It's an easy sale because of Rails.

For someone who knows Java and some functional programming (whether from Scala, Scheme, ML, or whatever), Ruby is pretty easy to learn. I was able to cover the basics in one lecture.

  • Duck typing
  • No braces but Algol-style if/loops/function definitions
  • Blocks
  • Arrays and hashes
  • Miscellaneous function stuff: optional (), optional return, varargs, named and default parameters
  • Class stuff: instance variables have @, attr_accessor, initialize,self

That's enough to be dangerous productive.

Then I prepared my lecture on metaprogramming, and I quickly said “holy cow!”. There are a dozen ways of doing the same thing slightly differently, and I was soon in a fog of mind-numbing detail and sample code that looked like random line noise. It took me a long time to pick a subset of the features that made this example work the way I wanted to.

That made me think about Scala. Why is it that Ruby walks on water but Scala tends to get a bad rap for complexity?

It's a little difficult for me to make an exact comparison because Ruby doesn't even have a language reference manual, but it seems to me that Ruby and Scala are pretty comparable in their “complexity budget”. Ruby spends it on an intricate metaobject protocol, Scala on an intricate type system.

I went back to last semester's slides to review my Scala crash course, and it wasn't all that different:

  • var/val/def
  • Types after names
  • Anonymous functions
  • Arrays, lists, and maps
  • Miscellaneous syntax stuff: optional (), optional ;
  • Class stuff: Fields give rise to getters/setters, primary and auxiliary constructors

And yes, that's enough to make you productive in Scala. You can merrily code along and use other people's libraries. Of course, you won't necessarily understand how they work under the hood, but then again, how many Rails users really know what makes ActiveRecord tick?

However, I sensed a significant difference in the culture of the Ruby and Scala crowd, at least as evidenced in the blog posts. The Ruby afficionados seem like a bunch of wizards trading spells. “Hey, try this one, it worked wonders for me!” The Scala discussions remind me of a faculty commons room, where everyone wants to show that they are the smartest one. “Well, if you would just take the monadic point of view, it would be obvious that...”

I exaggerate, of course, and I am glad that Scala attracts the smartest ones who, after all, build the stuff that we all get to use without knowing how it really works.



Related Topics >>


Ruby, Scala, and Complexity

Hi Professor Horstmann. I'm really happy you used the book I

recommended for your class :). I have a great amount of respect for
Scala (and Martin for writing it). With the perspective of using Ruby
everyday at my job, I'd like to point out that only in the rarest cases
do we use metaprogramming. Perhaps the libaries we use frequently like
ActiveRecord do place heavy utilization of method_missing, but I would
argue that that is probably one of the easiest-to-explain uses. I
haven't used Scala much beyond your courses, but when I did, I was
forced to deal with the type system the second Scala couldn't figure out
the type of my declaration. At least teaching Ruby, you can defer the
metaprogramming craziness until later. I still have to crack open a book
when I dive into that stuff and I've been working at my job for nearly 2
years now and feel like a pretty competent Ruby programmer.

With that said, I think if Ruby didn't exist, I would be a Scala
programmer :).

Just my two cents.

Ruby, Scala, and Complexity

Hi Glenn! I think the advanced programming class doesn't give a very accurate impression what it is like to use a language on a daily basis. When I use Scala to get a job done, I don't think about higher kinded types, just like you don't think about metaprogramming in your day-to-day Ruby work. If we had covered Ruby when you took my class, you'd now be a Scala programmer :-)

My point is that you can be a productive Ruby programmer without worrying about all the subtleties of the MOP, and you can be a productive Scala programmer without worrying about all the subtleties of the type system.

I think hat's a pretty

I think hat's a pretty insightful blogpost, Cay! I'm glad that the Scala community attracts so many wicked smart people, but sometimes the level of discussion does go right into the stratosphere :-). Interestingly, even though it may sometimes sound like a faculty common room, it's usually not the few academics among us who lead those discussions. We are far too busy and concerned with explaining Scala to newcomers...