Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Ruby more suitable for Rails than Python? [closed]

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?

like image 706
Victor Yan Avatar asked Jul 08 '09 16:07

Victor Yan


People also ask

Is Ruby on Rails better than Python?

Ruby on Rails or Python? Whether you select Python or Ruby on Rails, both are exceptional programming languages for web development with proficient procedures and a vast community. Python must be preferred where big data is included. While Ruby on Rails works great with high-traffic applications.

What are the advantages of Ruby over Python?

Advantages of RubyRuby has a clean and easy syntax, which allows a new developer to learn very quickly and easily. Just like Python, it's open source. Ruby language was developed to make the developer's work faster, and it gives freedom to developers to develop any size of the web app in shorter time duration.

Is Ruby on Rails easier than Python?

Ruby is more organized than Perl and more object-oriented than Python. Metaprogramming is a feature in Ruby and many other languages where the program is able to predict and auto-write code for you based on a developer's beginning input. This makes coding in Ruby even easier.

Why Ruby on Rails is better than Django?

In the battle of Django vs Ruby on Rails performance, it is observed that Rails is faster by 0.7%. It is because Rails has the advantage of a rich repository of amazing libraries and plugins to enhance the speed and eventually the performance of this framework.


2 Answers

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.

like image 75
Yehuda Katz Avatar answered Oct 03 '22 00:10

Yehuda Katz


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!

like image 45
Vinay Sajip Avatar answered Oct 03 '22 02:10

Vinay Sajip