CSC/ECE 517 Fall 2009/wiki1a 11 AS: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 47: Line 47:
== Advantages over statically typed languages ==
== Advantages over statically typed languages ==


1.Java has static typing. You declare the type of each variable, and then, during compilation, you get an error message if you use a variable of the wrong type. Ruby, on the other hand, has dynamic typing: You don't declare types for variables or functions, and no type-check occurs until runtime, when you get an error if you call a method that doesn't exist. Even then, Ruby doesn't care about an object's class, just whether it has a method of the name used in the method call. For this reason, the dynamic approach has earned the name duck typing: "If it walks like a duck and quacks like a duck, it's a duck."
1.Java has static typing. We declare the type of each variable, and then, during compilation, we get an error message if we use a variable of the wrong type. Ruby, on the other hand, has dynamic typing: We don't declare types for variables or functions, and no type-check occurs until runtime, when we get an error if we call a method that doesn't exist. Even then, Ruby doesn't care about an object's class, just whether it has a method of the name used in the method call. For this reason, the dynamic approach has earned the name duck typing: "If it walks like a duck and quacks like a duck, it's a duck."
   
   
'''Duck Typing'''
'''Duck Typing'''

Revision as of 01:25, 7 September 2009



Ruby vs Python

Definition of Ruby and Python

Ruby is a dynamic, reflective, general purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features.

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.

Differences in their language features

The first thing to be noted in Ruby is that in Ruby everything is an object, whereas in Python you have the liberty to code without using objects.

// include example here

In ruby, the programmer can use blocks to extend the language for application specific control statements where as in Python you are supposed to use only the control statements provided by the language.


Differences in Programming Environment

Python has a Virtual Machine which optimizes certain functions like hash functions and sorting algorithms.

Ruby as such does not have any virtual machine but it acts as an interpreter. There is a project going on for building a virtual machine for Ruby- YARV [1]

What Ruby has and Python does not have?

Ruby has continuations, where as Python does not have it. One notable use of continuations in Ruby is for breakpoints in debuggers. However you can fake continuations in Python, and it seems that many of the use cases can instead be done with passing variables into .next() which you can do in Python 3.1. Hence, the use of continuations seems rather limited, and hard to understand, but it’s there in Ruby, and not in Python. So this is another plus for Ruby.

//examples must be included

What Python has and Ruby does not have?

1. It’s worth mentioning here that Python has some big features that Ruby doesn’t, like for example list comprehensions.

 [foo(x) for x in alist if bar(x) != 'frotz']

This is definitely shorter than:

  foo = [] 
  for x in alist:
  if bar(x) != 'frotz':
  foo.append(x)

2.

Advantages over statically typed languages

1.Java has static typing. We declare the type of each variable, and then, during compilation, we get an error message if we use a variable of the wrong type. Ruby, on the other hand, has dynamic typing: We don't declare types for variables or functions, and no type-check occurs until runtime, when we get an error if we call a method that doesn't exist. Even then, Ruby doesn't care about an object's class, just whether it has a method of the name used in the method call. For this reason, the dynamic approach has earned the name duck typing: "If it walks like a duck and quacks like a duck, it's a duck."

Duck Typing

class ADuck
    def quack()
        puts "quack A";
    end
end
class BDuck
    def quack()
        puts "quack B";
    end
end
# quack_it doesn't care about the type of the argument duck, as long
# as it has a method called quack. Classes A and B have no
# inheritance relationship.
def quack_it(duck)
    duck.quack
end
a = ADuck.new
b = BDuck.new
quack_it(a)
quack_it(b)


2.In Ruby, variables do not need to be declared and are free to change type from statement to statement. So the following code, where the variable x changes from a FixNum (an integer that fits within a native machine word) to a String to an Array, is a perfectly legal sequence of Ruby code:


x = 10
x += 4
x = "My String"
x = [1, "My String", Hash.new ]

3.Ruby's dynamic typing means you don't repeat yourself: In Java have we had to suffer through verbose code along the lines of

 XMLPersistence xmlPersistence = (XMLPersistence)persistenceManager.getPersistence(); 

Ruby eliminates the need for the type declaration and casting (as well as parentheses and semicolon): a typical Ruby equivalent would be

 xmlPersistence = persistence_manager.persistence.

Projects better suited for Ruby

The most prominent application of Ruby is in web development using Ruby on Rails. Ruby on Rails is an open source web framework. Rails was created by David Heinemeier Hansson in 2003. Since then many applications have been created using Ruby on Rails and presently this has been used in various applications like twitter [2] e-commerce[3], live video streaming [4], group chat application [5].

Projects better suited for Python

Python has two web development frameworks: Django and TurboGears. Apart from web development, Python also provides platform for applications like Database Access, Desktop Graphical User Interface(GUI) development, Software development, Game and 3D graphics development. One of the best examples of open source Python language is the "Zone Application server" [6].