CSC/ECE 517 Fall 2009/wiki1b 10 sf: Difference between revisions
No edit summary |
No edit summary |
||
(15 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
=Scala vs Ruby Introduction= | |||
This wiki page compares Ruby and Scala from programming perspectives like ease of coding , object orientedness , frameworks available etc. | |||
=Scala= | =Scala= | ||
It was started in 2001 at the École Polytechnique Fédérale de Lausanne (EPFL) by Martin Odersky . It unifies | It was started in 2001 at the École Polytechnique Fédérale de Lausanne (EPFL) by Martin Odersky . It unifies [http://www.c2.com/cgi/wiki?FunctionalProgramming Functional Programming] and [http://java.sun.com/docs/books/tutorial/java/concepts/ 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 | Scala is extensible because it allows to integrate new language constructs in form of libraries. It is capable of running both on the Java platform and .NET platform. | ||
The interpreter behind Scala is called ‘scalac’. It generates java | The interpreter behind Scala is called ‘scalac’. It generates java bytecode which can be run on the JVM. | ||
=Ruby= | =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. | 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 [http://www.tutorialspoint.com/ruby/ruby_modules.htm 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 also has features like exception handling, garbage collection, OS independent threading and is highly portable. Ruby can be embedded in C language. | ||
Line 14: | Line 17: | ||
==Statically typed vs Dynamic== | ==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 | Scala is [http://www.tutorialspoint.com/ruby/ruby_modules.htm 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 that is typed at the command prompt -: | ||
scala> val msg = "Hello, world!" | scala> val msg = "Hello, world!" | ||
Line 22: | Line 25: | ||
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. | 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 | Ruby on the other hand is a [http://www.yourdictionary.com/computer/dynamic-programming-language dynamic language]. | ||
def | class Human | ||
def identify | |||
puts "I am a person" | |||
end | |||
end | end | ||
puts | class Student < Human | ||
def identify | |||
puts "I am a student" | |||
end | |||
end | |||
Human.new.identify | |||
Student.new.identify | |||
Output -: | |||
I am a person | |||
I am a student | |||
Here we can see that the identify method to be called is decided at runtime. | |||
==Ease of Use== | ==Ease of Use== | ||
Line 52: | Line 67: | ||
=> true | => true | ||
Whereas Scala does not allow you to leave off parenthesis. For example -: | Whereas, Scala does not allow you to leave off parenthesis. For example -: | ||
Line 61: | Line 76: | ||
res5: Boolean = true | res5: Boolean = true | ||
Now, if you write in the following manner you will get an error. | |||
scala> s.contains "el" | scala> s.contains "el" | ||
Line 84: | Line 99: | ||
==Regular Expressions== | ==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. | Scala does not have [http://www.regular-expressions.info/ regular expressions] at the language level whereas Ruby has it which makes it easier to parse text/strings in Ruby. Click [http://xkcd.com/208/ here] for a webcomic on regular expressions. | ||
==Unbounded Polymorphism== | ==Unbounded Polymorphism== | ||
Line 93: | Line 108: | ||
Ruby supports class extensions by having open classes. Scala on the other hand supports implicit type conversion. | 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 -: | 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 | class String | ||
Line 105: | Line 120: | ||
In this code print_self is being added to the String class and hence can subsequently be invoked on String objects. | 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 | 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) | implicit def str2int(str:String):Int = Integer.parseInt(str) | ||
Line 111: | Line 126: | ||
addTwo("123", 456) | 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. | 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== | ==Functions== | ||
Line 119: | Line 134: | ||
==Speed and Performance== | ==Speed and Performance== | ||
Scala is faster than Ruby because the Scala interpreter creates Java | Scala is faster than Ruby because the Scala interpreter creates Java bytecode behind the scene, that run on the JVM. | ||
=Frameworks= | =Frameworks= | ||
Ruby frameworks | |||
* Rails | * Rails | ||
* Nitro | * Nitro | ||
Line 130: | Line 145: | ||
* Camping | * Camping | ||
Scala frameworks | |||
* Lift | * Lift | ||
* Sweet | * Sweet | ||
Line 137: | Line 151: | ||
* Pinky | * Pinky | ||
= | =Conclusion= | ||
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. | 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 | 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 bytecode 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. | There is an open debate as to which language is superior than the other. Both have their own advantages as well as disadvantages. | ||
Line 146: | Line 160: | ||
* About Ruby: http://www.ruby-lang.org/en/about/ | * About Ruby: http://www.ruby-lang.org/en/about/ | ||
* Redefinition of methods in Ruby: http://www.rubyist.net/~slagell/ruby/redefinemethods.html | |||
* About Scala: http://www.scala-lang.org/node/25 | * About Scala: http://www.scala-lang.org/node/25 | ||
* Google TechTalk on Scala: http://video.google.com/videoplay?docid=553859542692229789# | * Google TechTalk on Scala: http://video.google.com/videoplay?docid=553859542692229789# | ||
Line 153: | Line 168: | ||
* Class Extensions and Operator overloading: http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-6 | * 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 | * 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 |
Latest revision as of 02:42, 29 September 2009
Scala vs Ruby Introduction
This wiki page compares Ruby and Scala from programming perspectives like ease of coding , object orientedness , frameworks available etc.
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 of running both on the Java platform and .NET platform. The interpreter behind Scala is called ‘scalac’. It generates java bytecode 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 that is typed at 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 a dynamic language.
class Human def identify puts "I am a person" end end
class Student < Human def identify puts "I am a student" end end
Human.new.identify Student.new.identify
Output -:
I am a person I am a student
Here we can see that the identify method to be called is decided at runtime.
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
Now, 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. Click here for a webcomic on regular expressions.
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 bytecode behind the scene, that run on the JVM.
Frameworks
Ruby frameworks
- Rails
- Nitro
- Ramaze
- Merb
- Camping
Scala frameworks
- Lift
- Sweet
- Web Flavor
- Pinky
Conclusion
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 bytecode 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/
- Redefinition of methods in Ruby: http://www.rubyist.net/~slagell/ruby/redefinemethods.html
- 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