Wednesday, April 3, 2013

Scala or Ruby - Which Is Most Sexy?

In computer science, the syntax of a programming language is the set of rules that define the combinations of symbols that are considered to be correctly structured programs in that language. The syntax of a language defines its surface form.

So, when we ask, "Which has the best syntax, Scala or Ruby?," we're really asking, "Which code looks better?" Good looking code is Sexy Code.

I'll try to be objective.

I'll add to the collection of examples below over time. So, keep coming back to this blog post to see which language wins the most SEXY Award points.

Your opinions are welcome.

Some people think fat is beautiful, but that's generally not the case when it comes to code.

Let's start off with showing how each language handles converting two arrays into one a hash, i.e., an array of key/value pairs.

Convert Two Arrays to a Hash


>> keys = [1, 2, 3] 
=> [1, 2, 3] 
>> vals = ['A', 'B', 'C'] 
=> ["A", "B", "C"] 
>> hash = Hash[] 
=> {1=>"A", 2=>"B", 3=>"C"}


scala> val keys = Array(1, 2, 3) 
keys: Array[Int] = Array(1, 2, 3) 
scala> val values = Array("A", "B", "C") 
values: Array[java.lang.String] = Array(A, B, C) 
scala> val map = 
map: scala.collection.immutable.Map[Int,java.lang.String] = Map(1 -> A, 2 -> B, 3 -> C)

Winner: Ruby

Ruby is the clear winner, based on KISS (Keep It Simple, Stupid). It takes less typing to accomplish the same goal. Less code means less to type and less to maintain.  In this example, Ruby is sexier than Scala.

Find Largest Value in Array


=> 3

Ruby, Similar to Scala (not great)

>> [1, 3, 2].reduce(0) {|max, value| max = value if value > max; max}
=> 3


scala> List(1, 3, 2).reduceLeft ( _ max _ )
res0: Int = 3

Winner: Ruby


Which ever language (Scala or Ruby) has the most, wins.


Most SEXY Award

Winner: Ruby

Sponsor Ads(Please visit one if you liked this article. Thanks!)


  1. Very nice. Except you can find the largest item in an array with Ruby much, much more concisely:

    [1,2,3].max => 3

  2. Thanks! Even better! I'll update the results.

  3. Yes, [1,2,3].max is clearly infinitely more sexy than List(1,2,3).max

  4. And for that matter, this is also horribly ugly:

    val a = Array(1,2,3)
    val b = Array("A", "B", "C")

    Map(a zip b: _*)

    Clearly inferior to Ruby on an unbiased sex scale.

  5. Wonderful article, thanks for putting this together! This is obviously one great post. Thanks for the valuable information and insights you have so provided here. Marijuana online