title: What does Ruby have that Python doesn't, and vice versa?
body: <p>There is a lot of discussions of Python vs Ruby, and I all find them completely unhelpful, because they all turn around why feature X sucks in language Y, or that claim language Y doesn't have X, although in fact it does. I also know exactly why I prefer Python, but that's also subjective, and wouldn't help anybody choosing, as they might not have the same tastes in development as I do.</p>
  
  <p>It would therefore be interesting to list the differences, objectively. So no "Python's lambdas sucks". Instead explain what Ruby's lambdas can do that Python's can't. No subjectivity. Example code is good!</p>
  
  <p>Don't have several differences in one answer, please. And vote up the ones you know are correct, and down those you know are incorrect (or are subjective). Also, differences in syntax is not interesting. We know Python does with indentation what Ruby does with brackets and ends, and that @ is called self in Python.</p>
  
  <p>UPDATE: This is now a community wiki, so we can add the big differences here.</p>
  
  <h2>Ruby has a class reference in the class body</h2>
  
  <p>In Ruby you have a reference to the class (self) already in the class body. In Python you don't have a reference to the class until after the class construction is finished.</p>
  
  <p>An example:</p>
  
  <pre><code>class Kaka
    puts self
  end
  </code></pre>
  
  <p>self in this case is the class, and this code would print out "Kaka". There is no way to print out the class name or in other ways access the class from the class definition body in Python (outside method definitions).</p>
  
  <h2>All classes are mutable in Ruby</h2>
  
  <p>This lets you develop extensions to core classes.  Here's an example of a rails extension:</p>
  
  <pre><code>class String
    def starts_with?(other)
      head = self[0, other.length]
      head == other
    end
  end
  </code></pre>
  
  <p>Python (imagine there were no <code>''.startswith</code> method):</p>
  
  <pre><code>def starts_with(s, prefix):
      return s[:len(prefix)] == prefix
  </code></pre>
  
  <p>You could use it on any sequence (not just strings). In order to use it you should import it <em>explicitly</em> e.g., <code>from some_module import starts_with</code>.</p>
  
  <h2>Ruby has Perl-like scripting features</h2>
  
  <p>Ruby has first class regexps, $-variables, the awk/perl line by line input loop and other features that make it more suited to writing small shell scripts that munge text files or act as glue code for other programs.</p>
  
  <h2>Ruby has first class continuations</h2>
  
  <p>Thanks to the callcc statement. In Python you can create continuations by various techniques, but there is no support built in to the language.</p>
  
  <h2>Ruby has blocks</h2>
  
  <p>With the "do" statement you can create a multi-line anonymous function in Ruby, which will be passed in as an argument into the method in front of do, and called from there. In Python you would instead do this either by passing a method or with generators.</p>
  
  <p>Ruby:</p>
  
  <pre><code>amethod { |here|
      many=lines+of+code
      goes(here)
  }
  </code></pre>
  
  <p>Python (Ruby blocks correspond to different constructs in Python):</p>
  
  <pre><code>with amethod() as here: # `amethod() is a context manager
      many=lines+of+code
      goes(here)
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>for here in amethod(): # `amethod()` is an iterable
      many=lines+of+code
      goes(here)
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>def function(here):
      many=lines+of+code
      goes(here)
  
  amethod(function)     # `function` is a callback
  </code></pre>
  
  <p>Interestingly, the convenience statement in Ruby for calling a block is called "yield", which in Python will create a generator.</p>
  
  <p>Ruby:</p>
  
  <pre><code>def themethod
      yield 5
  end
  
  themethod do |foo|
      puts foo
  end
  </code></pre>
  
  <p>Python:</p>
  
  <pre><code>def themethod():
      yield 5
  
  for foo in themethod():
      print foo
  </code></pre>
  
  <p>Although the principles are different, the result is strikingly similar.</p>
  
  <h2>Ruby supports functional style (pipe-like) programming more easily</h2>
  
  <pre><code>myList.map(&amp;:description).reject(&amp;:empty?).join("\n")
  </code></pre>
  
  <p>Python:</p>
  
  <pre><code>descriptions = (f.description() for f in mylist)
  "\n".join(filter(len, descriptions))
  </code></pre>
  
  <h2>Python has built-in generators (which are used like Ruby blocks, as noted above)</h2>
  
  <p>Python has support for generators in the language. In Ruby 1.8 you can use the generator module which uses continuations to create a generator from a block. Or, you could just use a block/proc/lambda! Moreover, in Ruby 1.9 Fibers are, and can be used as, generators, and the Enumerator class is a built-in generator <a href="http://wiki.github.com/rdp/ruby_tutorials_core/enumerator" rel="nofollow noreferrer">4</a></p>
  
  <p><a href="http://docs.python.org/tutorial/classes.html#generators" rel="nofollow noreferrer">docs.python.org</a> has this generator example:</p>
  
  <pre><code>def reverse(data):
      for index in range(len(data)-1, -1, -1):
          yield data[index]
  </code></pre>
  
  <p>Contrast this with the above block examples.</p>
  
  <h2>Python has flexible name space handling</h2>
  
  <p>In Ruby, when you import a file with <code>require</code>, all the things defined in that file will end up in your global namespace. This causes namespace pollution. The solution to that is Rubys modules. But if you create a namespace with a module, then you have to use that namespace to access the contained classes.</p>
  
  <p>In Python, the file is a module, and you can import its contained names with <code>from themodule import *</code>, thereby polluting the namespace if you want. But you can also import just selected names with <code>from themodule import aname, another</code> or you can simply <code>import themodule</code> and then access the names with <code>themodule.aname</code>. If you want more levels in your namespace you can have packages, which are directories with modules and an <code>__init__.py</code> file.</p>
  
  <h2>Python has docstrings</h2>
  
  <p>Docstrings are strings that are attached to modules, functions and methods and can be
  introspected at runtime. This helps for creating such things as the help command and
  automatic documentation.</p>
  
  <pre><code>def frobnicate(bar):
      """frobnicate takes a bar and frobnicates it
  
         &gt;&gt;&gt; bar = Bar()
         &gt;&gt;&gt; bar.is_frobnicated()
         False
         &gt;&gt;&gt; frobnicate(bar)
         &gt;&gt;&gt; bar.is_frobnicated()
         True
      """
  </code></pre>
  
  <p>Ruby's equivalent are similar to javadocs, and located above the method instead of within it.  They can be retrieved at runtime from the files by using 1.9's Method#source_location <a href="http://github.com/rdp/ri_for" rel="nofollow noreferrer" title="example use">example use</a></p>
  
  <h2>Python has multiple inheritance</h2>
  
  <p>Ruby does not ("on purpose" -- see Ruby's website, <a href="http://codeidol.com/other/rubyckbk/Modules-and-Namespaces/Simulating-Multiple-Inheritance-with-Mixins/" rel="nofollow noreferrer">see here how it's done in Ruby</a>). It does reuse the module concept as a type of abstract classes.</p>
  
  <h2>Python has list/dict comprehensions</h2>
  
  <p>Python:</p>
  
  <pre><code>res = [x*x for x in range(1, 10)]
  </code></pre>
  
  <p>Ruby:</p>
  
  <pre><code>res = (0..9).map { |x| x * x }
  </code></pre>
  
  <p>Python:</p>
  
  <pre><code>&gt;&gt;&gt; (x*x for x in range(10))
  &lt;generator object &lt;genexpr&gt; at 0xb7c1ccd4&gt;
  &gt;&gt;&gt; list(_)
  [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
  </code></pre>
  
  <p>Ruby:</p>
  
  <pre><code>p = proc { |x| x * x }
  (0..9).map(&amp;p)
  </code></pre>
  
  <p>Python <strong>2.7+</strong>:</p>
  
  <pre><code>&gt;&gt;&gt; {x:str(y*y) for x,y in {1:2, 3:4}.items()}
  {1: '4', 3: '16'}
  </code></pre>
  
  <p>Ruby:</p>
  
  <pre><code>&gt;&gt; Hash[{1=&gt;2, 3=&gt;4}.map{|x,y| [x,(y*y).to_s]}]
  =&gt; {1=&gt;"4", 3=&gt;"16"}
  </code></pre>
  
  <h2>Python has decorators</h2>
  
  <p>Things similar to decorators can also be created in Ruby, and it can also be argued that they aren't as necessary as in Python.</p>
  
  <h2>Syntax differences</h2>
  
  <p>Ruby requires "end" or "}" to close all of its scopes, while Python uses white-space only.  There have been recent attempts in Ruby to allow for whitespace only indentation <a href="http://github.com/michaeledgar/seamless" rel="nofollow noreferrer">http://github.com/michaeledgar/seamless</a></p>