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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 6: Line 6:


   
   
=Ruby=
=Ruby=


Line 14: Line 13:
   
   
=Ruby Vs Scala=
=Ruby Vs Scala=


==Statically typed vs Dynamic==
==Statically typed vs Dynamic==
Line 123: Line 121:


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 classes behind the scene, that run on the JVM.


=Frameworks=
=Frameworks=

Revision as of 23:54, 19 September 2009

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

Also, 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. (put link here for 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

The frameworks for Scala are the following -: