92

Python and Ruby are usually considered to be close cousins (though with quite different historical baggage) with similar expressiveness and power. But some have argued that the immense success of the Rails framework really has a great deal to do with the language it is built on: Ruby itself. So why would Ruby be more suitable for such a framework than Python?

18
  • 45
    Alliteration. _
    – Jimmy
    Jul 8, 2009 at 17:04
  • 75
    "Python on Pails" just doesn't have the same feel to it...
    – ephemient
    Jul 8, 2009 at 17:06
  • 105
    @Ephemient: I believe it would be Python on Planes.
    – Jimmy
    Jul 8, 2009 at 17:13
  • 37
    @Jimmy: Who needs planes? import antigravity ;-) xkcd.com/353 Jul 8, 2009 at 19:00
  • 157
    Is there a Java in Jails?
    – Nosredna
    Jul 9, 2009 at 2:00

13 Answers 13

172

There are probably two major differences:

Ruby has elegant, anonymous closures.

Rails uses them to good effect. Here's an example:

class WeblogController < ActionController::Base
  def index
    @posts = Post.find :all
    respond_to do |format|
      format.html
      format.xml { render :xml => @posts.to_xml }
      format.rss { render :action => "feed.rxml" }
    end
  end
end

Anonymous closures/lambdas make it easier to emulate new language features that would take blocks. In Python, closures exist, but they must be named in order to be used. So instead of being able to use closures to emulate new language features, you're forced to be explicit about the fact that you're using a closure.

Ruby has cleaner, easier to use metaprogramming.

This is used extensively in Rails, primarily because of how easy it is to use. To be specific, in Ruby, you can execute arbitrary code in the context of the class. The following snippets are equivalent:

class Foo
  def self.make_hello_method
    class_eval do
      def hello
        puts "HELLO"
      end
    end
  end
end

class Bar < Foo # snippet 1
  make_hello_method
end

class Bar < Foo; end # snippet 2
Bar.make_hello_method

In both cases, you can then do:

Bar.new.hello  

which will print "HELLO". The class_eval method also takes a String, so it's possible to create methods on the fly, as a class is being created, that have differing semantics based on the parameters that are passed in.

It is, in fact, possible to do this sort of metaprogramming in Python (and other languages, too), but Ruby has a leg up because metaprogramming isn't a special style of programming. It flows from the fact that in Ruby, everything is an object and all lines of code are directly executed. As a result, Classes are themselves objects, class bodies have a self pointing at the Class, and you can call methods on the class as you are creating one.

This is to large degree responsible for the degree of declarativeness possible in Rails, and the ease by which we are able to implement new declarative features that look like keywords or new block language features.

21
  • 40
    In Python, everything is objects and all lines of code are directly executed too. ;) But, you don't have a "self" pointing at the class in the class body, that doesn't get created until after the class definition, so you have to put that code afterwards in Python, which admittedly is less elegant, but functionally equivalent. Jul 8, 2009 at 17:19
  • 32
    @lennart that's kind of the point. Python allows you to do the same kinds of things with named lambdas, decorators, and putting code after classes are created, but the loss in elegance adds up quickly and makes something like Rails either noticeably harder to implement or noticeably less elegant for end users. Jul 8, 2009 at 18:27
  • 9
    They don't really look like major differences to me. Jul 8, 2009 at 20:07
  • 10
    @lennart I'm a bit confused. I said that you didn't need them in Python -- but that not having them made the code harder to implement or less elegant for end users (one or the other). The languages are turing complete--you can write Rails in C if you want. Jul 9, 2009 at 6:50
  • 5
    @lennart Now we're getting into subjective territory, but the two features I talked about are quite convenient when producing frameworks with a mix of declarative and procedural programming. The lack of anonymous lambdas, in particular, is an expressiveness limitation of Python. The lack of consistency (the need to work with created classes only AFTER classes were created) is quite limiting as well. Jul 10, 2009 at 14:31
58

Those who have argued that

the immense success of the Rails framework really has a great deal to do with the language it is built on

are (IMO) mistaken. That success probably owes more to clever and sustained marketing than to any technical prowess. Django arguably does a better job in many areas (e.g. the built-in kick-ass admin) without the need for any features of Ruby. I'm not dissing Ruby at all, just standing up for Python!

17
  • 10
    Well, we're getting into subjective territory here. If you think the admin is an "only", then perhaps it's because you have not enjoyed the time-saving benefits it confers. Are there any areas that you think Django does worse than Rails, because of features which Ruby has and Python doesn't? The point really isn't about which framework is better - it's whether (as pointed out elsewhere in this question) there's anything lacking in Python which makes it less capable of developing a kick-ass framework. On the evidence, there's no such lack. Jul 8, 2009 at 18:56
  • 18
    @To the downvoters: I don't really mind, but I'm curious to know why you thought that my answer was unhelpful. I didn't realise one downvoted because one disagreed with someone's position - I generally have downvoted only where I felt that a question or answer was somehow making things worse. Jul 8, 2009 at 23:15
  • 5
    I can write my own admin section, I don't need this is in framework. I prefer other ways of making my application easier to write.
    – nitecoder
    Jul 9, 2009 at 4:40
  • 8
    @railsninja: Good for you. I prefer not to have to write boilerplate pages for admin housekeeping chores that most systems need. Recently, I did some pro-bono work for a local charity website, and it wouldn't have been feasible to do that site at all if the Django admin were not part of the equation. As it was, I provided a site with a fairly customised Ajaxified UI for the end users, but back end administrators worked with the admin and it was more than adequate for their needs. Jul 9, 2009 at 5:14
  • 6
    @Matt: His question is why Ruby is MORE suitable than Python.. And the answer, quite correctly, is that it isn't. Jul 10, 2009 at 7:40
54

The python community believes that doing things the most simple and straight forward way possible is the highest form of elegance. The ruby community believes doing things in clever ways that allow for cool code is the highest form of elegance.

Rails is all about if you follow certain conventions, loads of other things magically happen for you. That jives really well with the ruby way of looking at the world, but doesn't really follow the python way.

12
  • 4
    Sure, but there are loss of Perl people (well, maybe not lots) who think that cryptic one-liners are cool, and many Lisp people who swear that it's the one true language. We're definitely in whatever-floats-your-boat territory. Jul 8, 2009 at 22:58
  • 4
    Rails has zero magic, it is right there in the source. If you want to know how, get off your ass and find out.
    – nitecoder
    Jul 9, 2009 at 4:41
  • 21
    "Any sufficiently advanced technology is indistinguishable from magic." - Arthur C. Clarke Jul 9, 2009 at 8:46
  • 1
    "magic" meaning the framework does a heck of a lot of things for you without being directly asked. Again, I am not making value judgments, it is one style of doing things that has good sides and bad sides. Personally, I think it works wonderfully in rails. Jul 9, 2009 at 12:24
  • 2
    Elegance and conventions do not denote magic.
    – BJ Clark
    Jul 12, 2009 at 4:29
26

Is this debate a new "vim versus emacs" debate?

I am a Python/Django programmer and thus far I've never found a problem in that language/framework that would lead me to switch to Ruby/Rails.

I can imagine that it would be the same if I were experienced with Ruby/Rails.

Both have similar philosophy and do the job in a fast and elegant way. The better choice is what you already know.

26

Personally, I find ruby to be superior to python in many ways that comprise what I'd call 'consistent expressiveness'. For example, in ruby, join is a method on the array object which outputs a string, so you get something like this:

numlist = [1,2,3,4]
#=> [1, 2, 3, 4]
numlist.join(',')
#=> "1,2,3,4"

In python, join is a method on the string object but which throws an error if you pass it something other than a string as the thing to join, so the same construct is something like:

numlist = [1,2,3,4]
numlist
#=> [1, 2, 3, 4]
",".join([str(i) for i in numlist])
#=> '1,2,3,4'

There are a lot of these little kinds of differences that add up over time.

Also, I cannot think of a better way to introduce invisible logic errors than to make whitespace significant.

18
  • 29
    My experience is that making whitespace significant helps logic errors go away. It's much more confusing to have spacing and syntax disagree.
    – Nosredna
    Jul 9, 2009 at 0:30
  • 5
    In languages with begins and ends and in languages with braces and in assembly, I've seen code get pasted in wrong and cause trouble later. That's always a problem. Have you had a lot of trouble with people pasting Python badly?
    – Nosredna
    Jul 9, 2009 at 1:56
  • 5
    Whitespace is not significant in Python: secnetix.de/~olli/Python/block_indentation.hawk . It's almost impossible to introduce "invisible errors" due to indentation in Python (you have to fudge your editor settings) while of course it is completely possible to introduce invisible errors due to indentation in any other language, simply by indenting incorrectly. @fields: So don't copy code via skype or HTML, then. Geez. Jul 9, 2009 at 5:10
  • 7
    It is correct that Python complains if you try to add non-strings to strings, like in a Join. This is because explicit is better than implicit. There are very few automatic conversions in Python, and the reason for that is that they tend to lead to problems, especially in dynamic languages, since things end up not being the type you expected it to be. Sure the "".join() method does feel backwards in the beginning, but that's the reason. It actually doesn't make sense on the list... Jul 9, 2009 at 5:14
  • 8
    God almighty... You mean statically typed, not strongly typed. Python is strongly typed, So is Ruby: stackoverflow.com/questions/520228/… You can't add a string to an integer in Ruby either. I'm getting tired of correcting you, please check your facts before you answer in the future. Jul 10, 2009 at 18:26
15

The real answer is neither Python or Ruby are better/worse candidates for a web framework. If you want objectivity you need to write some code in both and see which fits your personal preference best, including community.

Most people who argue for one or other have either never used the other language seriously or are 'voting' for their personal preference.

I would guess most people settle on which ever they come in to contact with first because it teaches them something new (MVC, testing, generators etc.) or does something better (plugins, templating etc). I used to develop with PHP and came in to contact with RubyOnRails. If I had have known about MVC before finding Rails I would more than likely never left PHP behind. But once I started using Ruby I enjoyed the syntax, features etc.

If I had have found Python and one of its MVC frameworks first I would more than likely be praising that language instead!

0
11

Python has a whole host of Rails-like frameworks. There are so many that a joke goes that during the typical talk at PyCon at least one web framework will see the light.

The argument that Rubys meta programming would make it better suited is IMO incorrect. You don't need metaprogramming for frameworks like this.

So I think we can conclude that Ruby are not better (and likely neither worse) than Python in this respect.

0
8

Because Rails is developed to take advantage of Rubys feature set.

A similarly gormless question would be "Why is Python more suitable for Django than Ruby is?".

4

I suppose we should not discuss the language features per se but rather the accents the respective communities make on the language features. For example, in Python, re-opening a class is perfectly possible but it is not common; in Ruby, however, re-opening a class is something of the daily practice. this allows for a quick and straightforward customization of the framework to the current requirement and renders Ruby more favorable for Rails-like frameworks than any other dynamic language. Hence my answer: common use of re-opening classes.

1

Some have said that the type of metaprogramming required to make ActiveRecord (a key component of rails) possible is easier and more natural to do in ruby than in python - I do not know python yet;), so i cannot personally confirm this statement.

I have used rails briefly, and its use of catchalls/interceptors and dynamic evaluation/code injection does allow you to operate at a much higher level of abstraction than some of the other frameworks (before its time). I have little to no experience with Python's framework - but i've heard it's equally capable - and that the python community does a great job supporting and fostering pythonic endeavors.

2
  • 3
    Indeed, this sort of "magic" is often frowned upon in Python; for example, code.djangoproject.com/wiki/RemovingTheMagic
    – ephemient
    Jul 8, 2009 at 17:03
  • 2
    I think "magic" (metaprogramming tricks) for the sake of "magic" should always be frowned upon - simpler code, but equally powerful and expressive, should always win - but there are cases when the only way to provide the exact functionality and syntax you want requires "magic" - and in those cases, the "magic" is indispendable ;) Jul 8, 2009 at 17:09
1

I think that the syntax is cleaner and Ruby, for me at least, is just a lot more "enjoyable"- as subjective as that is!

0
-2

Two answers :

a. Because rails was written for ruby.

b. For the same reason C more suitable for Linux than Ruby

1
  • Given the context of the question answer makes absolutely no sense.
    – lorefnon
    Aug 4, 2012 at 23:01
-6

All of this is TOTALLY "IMHO"

In Ruby there is ONE web-application framework, so it is the only framework that is advertised for that language.

Python has had several since inception, just to name a few: Zope, Twisted, Django, TurboGears (it itself a mix of other framework components), Pylons (a kinda-clone of the Rails framework), and so on. None of them are python-community-wide supported as "THE one to use" so all the "groundswell" is spread over several projects.

Rails has the community size solely, or at least in the vast majority, because of Rails.

Both Python and Ruby are perfectly capable of doing the job as a web applications framework. Use the one YOU (and your potential development team) like and can align on.

6
  • 7
    ruby has multiple web application frameworks: Nitro, Merb, Camping.. to name a few Jul 8, 2009 at 21:49
  • 5
    Add: Sinatra and even a bare Rack app for very fast very minimal web apps as well.
    – Kris
    Jul 9, 2009 at 14:24
  • 2
    -1 "In Ruby there is ONE web-application framework" too categorical... for a false statement. Nitro, Merb, Camping, Sinatra Jul 10, 2009 at 15:14
  • 2
    Uninformed opinions from either side are exactly the cause of confusion for newcomers who are trying to sort all this out. It also means one could be missing out on something they would actually appreciate if they knew better.
    – Walt Jones
    Jul 14, 2009 at 15:46
  • 3
    I think his point was that Rails has a large mindshare of the Ruby community than Django has of the Python community, which is valid
    – pjb3
    Jul 27, 2009 at 18:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.