CSC/ECE 517 Fall 2009/wiki1b 10 sf
Scala
It was started in 2001 at the École Polytechnique Fédérale de Lausanne (EPFL) by Martin Odersky . It unifies ‘Functional Programming’ and ‘Object Oriented Programming’. It is object-oriented in the sense that everything in Scala is an object. The functionality and behavior is grouped into classes and traits and subclassing can be done by extending the same. In Scala every function returns a value. Functions can be anonymous, nested or curried. Scala is extensible because it allows to integrate new language constructs in form of libraries. It is capable if running both on the Java platform and .NET platform. The interpreter behind Scala is called ‘scalac’. It generates java classes which can be run on the JVM.
Ruby
It was first released in December 1995 and was created by Yukihiro Matsumoto. It is an object oriented language in which data types and classes are objects. Ruby supports only single inheritance. It does not support multiple inheritance but modules and mixins can be used to import functionality of other classes. Ruby also has features like exception handling, garbage collection, OS independent threading and is highly portable. Ruby can be embedded in C language.
Ruby Vs Scala
Statically typed vs Dynamic
Scala is statically typed wherein each variable, method parameter, return type etc has one type assigned at compile time and that dose not change. Look at the piece of code shown below types on the command prompt -:
scala> val msg = "Hello, world!" msg: java.lang.String = Hello, world!
When we assign “Hello,world!” to ‘msg’ the scala compiler automatically associates message with string data type as shown by the output in the next line. Hence when the code runs in the JVM, ‘msg’ is a string data type.
Ruby on the other hand is dynamically typed which means a variables type can changes at run time.
def say_goodnight(name) result = "Good Night, " + name result = 10 return result end
puts say_goodnight("xyz")
The output of this code spippet shown above will be 10 and not ‘Good Night, xyz’. As you can see the variable result can be assigned a value which can be a string or an integer.
Ease of Use
Although both Scala and Ruby are almost the same in terms of ease of use but there are some finer points of differences.For instance -:
Ruby allows you to leave off parenthesis when invoking a method. For example-:
>> s = "hello"
You can determine whether it contains a substring in the following manner -:
>> s.include?("el") => true
An alternate way of doing this is -:
>> s.include? "el" => true
Whereas Scala does not allow you to leave off parenthesis. For example -:
scala> val s = "hello" s: java.lang.String = hello
scala> s.contains("el") res5: Boolean = true
Whereas if you write in the following manner you will get an error.
scala> s.contains "el" :1: error: ';' expected but string literal found. s.contains "el" ^
However, Scala supports an "operator notation" which allows you to leave off both the dot and the parentheses. For example-:
scala> s contains "el" res6: Boolean = true
On the other hand, Ruby does not support this. For example -:
>> s include? "el"
(irb):21: warning: parenthesize argument(s) for future version NoMethodError: undefined method `include?' for main:Object from (irb):21
Regular Expressions
Scala does not have regular expressions at the language level whereas Ruby has it which makes it easier to parse text/strings in Ruby.
Unbounded Polymorphism
Ruby is a dynamic language and hence supports unbounded polymorphism through duck typing whereas Scala cannot support duck typing.Duck Typing Example
Class Extensions
Ruby supports class extensions by having open classes. Scala on the other hand supports implicit type conversion. In ruby classes are not enclosed. More methods can be added to pre defined classes by simply opening up the class and adding the methods in there.For example -:
class String def print_self puts self end end
"Daniel Spiewak".print_self # prints my name
In this code print_self is being added to the String class and hence can subsequently be invoked on String objects.
In Scala if an object of a certain data type, is passed to a method that does not take such a data type as an input, then the compiler looks around for a method that converts the unsupported data type to a data type that can be passed to the method. It implicitly uses the method to convert an object from an unsupported data type to the supported data type and then calls the function on this object.For example -:
implicit def str2int(str:String):Int = Integer.parseInt(str) def addTwo(a:Int, b:Int) = a + b addTwo("123", 456)
In the example shown above although the user passes a string to the addTwo method, the compiler looks around and finds the str2int method that converts string to an integer and passes the integer as the first parameter to the addTwo function.
Functions
Every function is a method is Ruby whereas functions are objects in Scala.
Speed and Performance
Scala is faster than Ruby because the Scala interpreter creates Java classes behind the scene, that run on the JVM.
Frameworks
The frameworks for Ruby are the following -:
- Rails
- Nitro
- Ramaze
- Merb
- Camping
The frameworks for Scala are the following -:
- .NET
- Lift
- Sweet
- Web Flavor
- Pinky
Conclusions
A study of Scala and Ruby helps in understanding the fact that although Scala is a statically typed language, it has the advantages of a dynamic language as well. Both of them have advanced language constructs that provide expressiveness and flexibility built into them. Both languages are concise and elegant. Scala does not have Regexs whereas Ruby has them which makes text parsing a lot more easier in Ruby. Ruby's open classes are easier to code than Scala's implicit type conversion. Ruby supports Duck Typing and hence is more flexible. Scala has an advantage that it is inter operable with Java. Since Scala interpreter creates Java classes behind the scene before running in the JVM, hence it makes it faster than Ruby. Scala has built-in syntactic support for XML and the XML classes are richer and much faster than Ruby's. There is an open debate as to which language is superior than the other. Both have their own advantages as well as disadvantages.
References
- About Ruby: http://www.ruby-lang.org/en/about/
- About Scala: http://www.scala-lang.org/node/25
- Google TechTalk on Scala: http://video.google.com/videoplay?docid=553859542692229789#
- First Steps to Scala: http://www.artima.com/scalazine/articles/steps.html
- Frameworks for Scala: http://blog.spiralarm.com/jono/2009/03/scala-frameworks.html
- Frameworks for Ruby: http://accidentaltechnologist.com/ruby/10-alternative-ruby-web-frameworks/
- Class Extensions and Operator overloading: http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-6
- Comparing DSL's in Ruby and Scala: http://www.artima.com/weblogs/viewpost.jsp?thread=251945
- Comparison of Ruby and Scala: http://blog.lostlake.org/index.php?/archives/27-5-Things-a-Ruby-developer-needs-to-know-about-Scala.html