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

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


In the case of Python, there are several web development frameworks such as Django, Grok, Pylons, TurboGears, web2py, Zope.[http://wiki.python.org/moin/WebFrameworks]
In the case of Python, there are several web development frameworks such as Django, Grok, Pylons, TurboGears, web2py, Zope.[http://wiki.python.org/moin/WebFrameworks]
Although Ruby on Rails wins hands down in popularity, Django is designed with similar features.


==Interesting Features Comparison==
==Interesting Features Comparison==

Revision as of 05:15, 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]

Although Ruby on Rails wins hands down in popularity, Django is designed with similar features.

Interesting Features Comparison

Advantages Over Statically Typed Languages

Comparison by Applications/Projects

References