Python vs Ruby
| Python | Ruby | |
|---|---|---|
|
VS
|
|
| http://www.python.org | http://www.ruby-lang.org |
Python and Ruby are two of the most popular dynamic programming languages used in high level application development. Developers often prototype using these two languages before implementing on compiled languages because of their modularity and object oriented features. Many use Python or Ruby instead of Perl as simple scripting languages. Python and Ruby are popular among web developers as well because of their rapid development cycle, with Python boasting computation efficiencies and Ruby boasting code design.
The latest stable version of Ruby is 2.0.0 released on 24 Feb 2013. The latest stable version(s) of Python are 3.3.0 released on September 29th, 2012, and 2.7.2 released on June 11th, 2011. Multiple current versions of Python often leads to confusion about selecting the correct version for a project. Most open source projects are written only to Python 2.x series of interpreters, whereas the most recent book publications are for the 3.x lineage.
Contents |
[edit] Philosophy
Python has been designed to emphasize programmer productivity and code readability. The Python syntax enforces strict indentation rules; in fact, indentation has semantic meaning in Python.
Ruby was designed, first and foremost, to make programming fun for its creator, with The Principle of Least Surprise and There's more than one way to do the same thing as guiding concepts:
Ruby inherited the Perl philosophy of having more than one way to do the same thing.
—Yukihiro Matsumoto (Matz) , The Philosophy of Ruby
This principle is the reason why many ruby methods have alternate names, which may lead to some API confusion among new practitioners. However this flexibility enables Ruby to be used as a meta language for describing DSL (Domain Specific Languages) such as the popular [Rails] MVC web framework, Rake, Cucumber and RSpec, among many others. In fact, this proliferation of DSLs has led to a massive surge of interest in Ruby and a loss to Python's mindshare in the last few years.
Python rejects the principle of There's more than one way to do the same thing. Instead it was reportedly designed according to the belief that There should be one — and preferably only one — obvious way to do it. This has since been reformulated as There's a most understandable way to do something and that is how it should be done.
[edit] Object Oriented Programming
Both Python and Ruby support object oriented programming. In Ruby, even global variables are actually embedded within the ObjectSpace object. Python doesn't have global variables, instead using attributes of module objects.
In Ruby, an instance of literally any type is an object. The same is true for Python. For instance, in Ruby, Class and Module instances are objects, with Class and Modules themselves being objects. However, where in Ruby all functions and most operators are in fact methods of an object, in Python functions are first-class objects themselves. A function in Python is typically declared as a member of another object (typically a Class, Instance, or Module), but can be freely passed around and attached to other objects at will.
[edit] Functional Programming
Both support some functional programming constructs, but Ruby is arguably better suited to a functional programming style. Lambdas in Python are generally very short, because lambdas in Python are restricted to expressions and cannot contain statements. Ruby's lambda support is far more flexible, allowing for lambda expressions of arbitrary length. In a similar vein, Python 2.x's closure support is more limited than Ruby's thanks to its limited support for lexical scope. Python 3 introduces the nonlocal keyword which addresses this limitation by allowing assignment to variables in outer non-global scopes.
On the other hand, Ruby 1.8, which is still used widely, does not support tail-call optimization for recursive functions, whereas Python uses a decorator to implement tail-call optimization. Ruby 1.9 — the current stable release — can currently be compiled to support tail-call optimization.
[edit] Speed
The standard CPython implementation is generally regarded as executing code slightly faster than Ruby, as of Ruby version 1.8. This can be attributed in part to the fact that Python can be compiled into "bytecode" (.pyc) before the code is run. However, major speed improvements have been made to Ruby 1.9, rendering the difference negligible.
Newer generation ruby parsers are working on improving Ruby's speed. The currently planned parser YARV is actually a VM that can also be used to execute precompiled bytecode.
On the Mac OS X platform, the MacRuby project offers an implementation of ruby 1.9 on top of the Objective-C runtime (including its garbage collector, the LLVM compiler infrastructure and more) which more than doubles the execution speed of Ruby code. Amongst other things, MacRuby maps most Ruby core types to native Cocoa objects (for example Array maps to NSMutableArray, while Hash maps to NSMutableDictionary in Cocoa) which provides a very noticeable improvement in execution speed.
If speed is really an issue for your Python project, you also have the option to use Cython, Pyrex,Pypy (JIT) or the ShedSkin tools to compile your code into C or C++.
[edit] Popularity
Python is generally more widely used than Ruby, according to most measures, but in the wake of the rising popularity of the Ruby on Rails Web application development framework Ruby's popularity too has seen rapid growth. Currently, on github , Ruby (14%) is only second to Javascript (21%) in popularity, followed by Python (8%). However looking at Sourceforge.net having more projects than github, Python has 13,628 compared to Ruby's 1,857 (as of 5JUL11). This is mainly due to ruby having its own rubygems.org location for projects. In recent years, the number of books pertaining to either of the languages has been rapidly increasing[citation needed]. There are now dozens of books on Ruby and on Python.
Python sees use in automating system administration and software development, web application development, analyzing scientific data (with help of numpy, scipy, and matplotlib modules), biostatisics, and teaching introductory computer science and programming.
Ruby sees use in web application development, and general programming.
Python also has come up with a web-framework named Django. Django is also getting equal attention as Rails. In terms of cloud deployment, Python can run on Google-Cloud (Google-App engine), which currently only supports Python, Java and Go. Though Ruby has very strong cloud deployment options in the shape of Heroku and Engine Yard.
[edit] Style
Ruby code is organized into blocks, with blocks starting with various constructs and ending with the keyword "end". Python code is indentation-sensitive, with successively larger indentation meaning tighter (nested) scopes. Python's syntax has been described as executable pseudocode.
Unlike most C derived languages, the boolean expression A < B < C is evaluated in Python as it would be mathematically. That is, it means A is less than B and B is less than C.
[edit] Features
Both languages are considered very high level languages. Each of them is estimated to have a Capers Jones language level of at least 15.
Both Python and Ruby (with 1.9) have full Unicode support, although the way that support is implemented varies.
Both Python and Ruby support multithreading. Python has what is called the Global Interpreter Lock (GIL), which negates much of the potential advantage of using threads; Ruby has a comparable Global VM Lock (GVL).
Ruby's object orientation is considered to be more 'pure' in that all functions exist inside a class of some sort. Python's object orientation is more akin to that of C++, which allows functions and statements that exist outside of classes.
There are a number of functions that are available by default in Ruby but for which in Python it is necessary to import a module from the standard library.
Python supports generators and generator expressions.
Ruby and Python both support the concept of metaclasses (a class is itself an object). Both also support class variables and methods (static methods/variables in C++).
[edit] Applications
There are a number of Web frameworks based on both Ruby and Python. The most notable are Ruby on Rails (Ruby) and Django (Python).

