Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are probably two major differences:</p> <h3>Ruby has elegant, anonymous closures.</h3> <p>Rails uses them to good effect. Here's an example:</p> <pre><code>class WeblogController &lt; ActionController::Base def index @posts = Post.find :all respond_to do |format| format.html format.xml { render :xml =&gt; @posts.to_xml } format.rss { render :action =&gt; "feed.rxml" } end end end </code></pre> <p>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.</p> <h3>Ruby has cleaner, easier to use metaprogramming.</h3> <p>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:</p> <pre><code>class Foo def self.make_hello_method class_eval do def hello puts "HELLO" end end end end class Bar &lt; Foo # snippet 1 make_hello_method end class Bar &lt; Foo; end # snippet 2 Bar.make_hello_method </code></pre> <p>In both cases, you can then do:</p> <pre><code>Bar.new.hello </code></pre> <p>which will print "HELLO". The <code>class_eval</code> 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.</p> <p>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, <code>Class</code>es are themselves objects, class bodies have a <code>self</code> pointing at the Class, and you can call methods on the class as you are creating one.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload