CSC/ECE 517 Fall 2009/wiki1b 10 sf: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(3 intermediate revisions by the same user 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 [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.
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 of running both on the Java platform and .NET platform.
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 classes which can be run on the JVM.
The interpreter behind Scala is called ‘scalac’. It generates java bytecode which can be run on the JVM.


=Ruby=
=Ruby=
Line 25: Line 28:


   class Human
   class Human
  def identify
    def identify
    puts "I am a person"
      puts "I am a person"
  end
    end
   end
   end


   class Student < Human
   class Student < Human
  def identify
    def identify
    puts "I am a student"
      puts "I am a student"
  end
    end
   end
   end


Line 131: Line 134:
==Speed and Performance==
==Speed and Performance==


Scala is faster than Ruby because the Scala interpreter creates Java classes behind the scene, that run on the JVM.
Scala is faster than Ruby because the Scala interpreter creates Java bytecode behind the scene, that run on the JVM.


=Frameworks=
=Frameworks=
Line 142: Line 145:
* Camping
* Camping


Scala framworks
Scala frameworks
* .NET
* Lift
* Lift
* Sweet
* Sweet
Line 152: Line 154:


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 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.
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 158: 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#

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