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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 112: Line 112:


As Rails is one of the most popular web development frameworks, it can be said that Ruby is more popular for web development that Python. Ruby on Rails makes a great framework to develop both small as well as high traffic sites. Ruby is, at present, exceptionally good at one specific kind of small project: database-backed web applications. Ruby on Rails counteracts all of Ruby's small-project disadvantages. [http://www.infoq.com/news/2007/06/rubyvsjava]
As Rails is one of the most popular web development frameworks, it can be said that Ruby is more popular for web development that Python. Ruby on Rails makes a great framework to develop both small as well as high traffic sites. Ruby is, at present, exceptionally good at one specific kind of small project: database-backed web applications. Ruby on Rails counteracts all of Ruby's small-project disadvantages. [http://www.infoq.com/news/2007/06/rubyvsjava]
Some of the top organizations using Ruby on Rails include NASA, Motorola, Google, Amazon, Cisco, IBM, Oracle, Yahoo.
[http://www.ruby-lang.org/en/documentation/success-stories/]
[http://blog.obiefernandez.com/content/2008/03/big-name-compan.html]
Python is better suited to desktop apps on Linux because you can perhaps integrate better to the host platform. [http://ubuntuforums.org/showthread.php?t=845563]


Python is believed to give better performance than Ruby and has a large standard library which is why more developers prefer it for areas such as text and numerical processing, operating system interfaces, internet protocols, internet security, software engineering, graphic design. Some of the big companies using Python include Google, Yahoo, CERN and NASA.
Python is believed to give better performance than Ruby and has a large standard library which is why more developers prefer it for areas such as text and numerical processing, operating system interfaces, internet protocols, internet security, software engineering, graphic design. Some of the big companies using Python include Google, Yahoo, CERN and NASA.
Line 117: Line 123:
[http://www.python.org/doc/faq/general/]
[http://www.python.org/doc/faq/general/]
[http://en.wikipedia.org/wiki/Python_(programming_language)]
[http://en.wikipedia.org/wiki/Python_(programming_language)]
Python is better suited to desktop apps on Linux because you can perhaps integrate better to the host platform. [http://ubuntuforums.org/showthread.php?t=845563]


==References==
==References==
{{Reflist}}
{{Reflist}}

Revision as of 23:16, 6 September 2009

Ruby vs. Python

Introduction

Python and Ruby are amongst some of the most popular programming languages in use today. When compared with the array of other languages these two seem almost similar but on closer observation we can find that they can be contrasted in various ways. There seems to be a continuous debate on trying to prove one over the other between their users.

Definitions

Python

Python is an object oriented programming language which supports scripting and was developed with the purpose of providing developers with a language which was both easy to code and read.

Ruby

Ruby is also an object oriented programming language with strong scripting support. It was introduced after python and hence primarily developed to incorporate perl-like scripting abilities with better object oriented functionality than Python.

Feature Comparison

Before we compare the two languages, maybe its a good idea to relate them! There are a lot of features that Ruby and Python have in common. Some of them are:

  • HighLevel ??
  • Garbage Collected ??
  • fully dynamic type system ??
  • automatic memory management ??

Now, let us see how these two languages differ in their features:

For a brief summary of differences, see table below.

Strings in Python vs. Strings in Ruby

String objects in Python are immutable whereas in Ruby strings are mutable objects.

An immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created.If an object is known to be immutable, it can be copied simply by making a copy of a reference to it instead of copying the entire object. Because a reference (typically only the size of a pointer) is usually much smaller than the object itself, this results in memory savings and a boost in execution speed.<ref>Immutable object</ref>

So in Python, adding a string to another can create several unwanted intermediate strings and consume memory. In Ruby however, strings can expand as required without consuming much memory or time.<ref>Perl Cookbook</ref>

Defining private variables and methods

http://johan.kiviniemi.name/blag/ruby-vs-python/ In Python "_" is appended in front of a variable or method name to make it private.

In Ruby, all variable defined in a class are by default private and no specific keyword is required to make them so. For making a method private, a method private is called just before the actual method.

private
  def sample_method_name

Lambdas vs. Code Blocks

In Ruby, a code block is an object which is contains certain code along with a context required to execute it. A code block can be thought of as a function not bound to a name. This is one of the most distinctive and unique features in Ruby. A code block in Ruby is defined as follows:

[1,2,3].each { |i| puts i}

http://www.devarticles.com/c/a/Ruby-on-Rails/Code-Blocks-and-Iteration/

In Python, the concept of code blocks does not exist but the ability of creating unnamed functions can be achieved using lambdas. The lambda function acts like any standard function in Python.

>>> (lambda x: x*2)(3)
8 

http://www.secnetix.de/olli/Python/lambda_functions.hawk


Syntax Differences

  • There’s public, private, and protected to enforce access, instead of Python’s _voluntary_ underscore __convention__.
  • Parentheses for method calls are usually optional.
  • You’ve got true and false instead of True and False (and nil instead of None).
  • It’s elsif instead of elif.
  • It’s require instead of import. Otherwise though, usage is the same.

http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-python/

Differences in (Web) Programming Environments

Web development in Ruby is done using the framework, Ruby on Rails. Ruby on Rails, often shortened to Rails or RoR, is an open source web application framework used with an Agile development methodology for rapid development. It uses the Model-View-Controller (MVC) architecture pattern to organize application programming.Ruby on Rails Considered to be one of the best web development frameworks in the market, Ruby on Rails has helped to substantially increase the popularity of Ruby.

In the case of Python, there are several web development frameworks such as Django, Grok, Pylons, TurboGears, web2py, Zope.[1]

Django is designed with a similar architecture and feature set to RoR to be just as efficient without being just a clone. So while either frameworks may be used to develop a good web application, there are certain differences in both these frameworks [3columns.net/habitual/docs/RailsVsDjango.pdf]:

  • Django has built in support for developer-friendly templates while RoR needs third party library support.
  • Rails has in-built javascript support while Django has no direct support.
  • Performance, scalability - Django better?
  • Lesser number of plugins and support available for Django compared to RoR.

RoR has for some time retained the top position in the popularity charts with developers, the situation seems to be changing with Django beating RoR. Django vs. Rails

Interesting Features Comparison

Advantages Over Statically Typed Languages

A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time. Statically typed languages include Ada, C, C++, C#, JADE, Java, Fortran, Haskell, ML, Pascal, Perl and Scala.[2]

static vs. dynamic

http://www.ferg.org/projects/python_java_side-by-side.html In Python, you never declare anything. An assignment statement binds a name to an object, and the object can be of any type. If a name is assigned to an object of one type, it may later be assigned to an object of a different type. That's what it means to say that Python is a dynamically typed language. Python container objects (e.g. lists and dictionaries) can hold objects of any type, including numbers and lists. When you retrieve an object from a container, it remembers its type, so no casting is required.

In Java, all variable names (along with their types) must be explicitly declared. Attempting to assign an object of the wrong type to a variable name triggers a type exception. That's what it means to say that Java is a statically typed language. Java container objects (e.g. Vector and ArrayList) hold objects of the generic type Object, but cannot hold primitives such as int. To store an int in a Vector, you must first convert the int to an Integer. When you retrieve an object from a container, it doesn't remember its type, and must be explicitly cast to the desired type.

Code size

Concise and compact vs. verbose

Python

print "Hello, world!"

Java

public class HelloWorld
{
   public static void main (String[] args)
   {
       System.out.println("Hello, world!");
   }
}

flexibility

flexibility of dynamically typed langauges makes writing code significantly easier. Modules are easier to write, and easier to change. There are no build time issues at all. [3]

  • java does not support operator overloading
  • generators (functions that save state between different results) not present in Java
  • supports metaprogramming as well as the procedural and functional paradigms.

Comparison by Applications/Projects

Since Ruby and Python are almost similar languages built with the same broad goals, we cannot pin point on certain applications that may be built only in Python or Ruby. Yet, due to the certain differences that exist developers may prefer one over the other depending on the applications requirements.

As Rails is one of the most popular web development frameworks, it can be said that Ruby is more popular for web development that Python. Ruby on Rails makes a great framework to develop both small as well as high traffic sites. Ruby is, at present, exceptionally good at one specific kind of small project: database-backed web applications. Ruby on Rails counteracts all of Ruby's small-project disadvantages. [4]

Some of the top organizations using Ruby on Rails include NASA, Motorola, Google, Amazon, Cisco, IBM, Oracle, Yahoo. [5] [6]

Python is better suited to desktop apps on Linux because you can perhaps integrate better to the host platform. [7]

Python is believed to give better performance than Ruby and has a large standard library which is why more developers prefer it for areas such as text and numerical processing, operating system interfaces, internet protocols, internet security, software engineering, graphic design. Some of the big companies using Python include Google, Yahoo, CERN and NASA. [8] [9] [10]

References