Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here are some key differences to me:</p> <ol> <li><p>Ruby has blocks; Python does not.</p></li> <li><p>Python has functions; Ruby does not. In Python, you can take any function or method and pass it to another function. In Ruby, everything is a method, and methods can't be directly passed. Instead, you have to wrap them in Proc's to pass them.</p></li> <li><p>Ruby and Python both support closures, but in different ways. In Python, you can define a function inside another function. The inner function has read access to variables from the outer function, but not write access. In Ruby, you define closures using blocks. The closures have full read and write access to variables from the outer scope.</p></li> <li><p>Python has list comprehensions, which are pretty expressive. For example, if you have a list of numbers, you can write</p> <pre><code>[x*x for x in values if x &gt; 15] </code></pre> <p>to get a new list of the squares of all values greater than 15. In Ruby, you'd have to write the following:</p> <pre><code>values.select {|v| v &gt; 15}.map {|v| v * v} </code></pre> <p>The Ruby code doesn't feel as compact. It's also not as efficient since it first converts the values array into a shorter intermediate array containing the values greater than 15. Then, it takes the intermediate array and generates a final array containing the squares of the intermediates. The intermediate array is then thrown out. So, Ruby ends up with 3 arrays in memory during the computation; Python only needs the input list and the resulting list.</p> <p>Python also supplies similar map comprehensions.</p></li> <li><p>Python supports tuples; Ruby doesn't. In Ruby, you have to use arrays to simulate tuples.</p></li> <li><p>Ruby supports switch/case statements; Python does not.</p></li> <li><p><del>Ruby supports the standard <code>expr ? val1 : val2</code> ternary operator; Python does not.</del></p></li> <li><p>Ruby supports only single inheritance. If you need to mimic multiple inheritance, you can define modules and use mix-ins to pull the module methods into classes. Python supports multiple inheritance rather than module mix-ins.</p></li> <li><p>Python supports only single-line lambda functions. Ruby blocks, which are kind of/sort of lambda functions, can be arbitrarily big. Because of this, Ruby code is typically written in a more functional style than Python code. For example, to loop over a list in Ruby, you typically do</p> <pre><code>collection.each do |value| ... end </code></pre> <p>The block works very much like a function being passed to <code>collection.each</code>. If you were to do the same thing in Python, you'd have to define a named inner function and then pass that to the collection each method (if list supported this method):</p> <pre><code>def some_operation(value): ... collection.each(some_operation) </code></pre> <p>That doesn't flow very nicely. So, typically the following non-functional approach would be used in Python:</p> <pre><code>for value in collection: ... </code></pre></li> <li><p>Using resources in a safe way is quite different between the two languages. Here, the problem is that you want to allocate some resource (open a file, obtain a database cursor, etc), perform some arbitrary operation on it, and then close it in a safe manner even if an exception occurs.</p> <p>In Ruby, because blocks are so easy to use (see #9), you would typically code this pattern as a method that takes a block for the arbitrary operation to perform on the resource.</p> <p>In Python, passing in a function for the arbitrary action is a little clunkier since you have to write a named, inner function (see #9). Instead, Python uses a <code>with</code> statement for safe resource handling. See <a href="https://stackoverflow.com/questions/865115/how-do-i-correctly-clean-up-a-python-object/865272#865272">How do I correctly clean up a Python object?</a> for more details.</p></li> </ol>
    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. 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