Learning Ruby from Python; Differences and Similarities

Learning Python from Ruby; Differences and Similarities

Here are some key differences to me:

  1. Ruby has blocks; Python does not.

  2. 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.

  3. 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.

  4. Python has list comprehensions, which are pretty expressive. For example, if you have a list of numbers, you can write

    [x*x for x in values if x > 15]

    to get a new list of the squares of all values greater than 15. In Ruby, you'd have to write the following:

    values.select {|v| v > 15}.map {|v| v * v}

    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.

    Python also supplies similar map comprehensions.

  5. Python supports tuples; Ruby doesn't. In Ruby, you have to use arrays to simulate tuples.

  6. Ruby supports switch/case statements; Python does not.

  7. Ruby supports the standard expr ? val1 : val2 ternary operator; Python does not.

  8. 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.

  9. 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

    collection.each do |value|
    ...
    end

    The block works very much like a function being passed to collection.each. 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):

    def some_operation(value):
    ...

    collection.each(some_operation)

    That doesn't flow very nicely. So, typically the following non-functional approach would be used in Python:

    for value in collection:
    ...
  10. 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.

    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.

    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 with statement for safe resource handling. See How do I correctly clean up a Python object? for more details.

Learning Ruby from Python; Differences and Similarities

While not targeted at Python programmers, you might find this idomatic Ruby talk useful.

(Related, but for Python: Code Like a Pythonista: Idiomatic Python)

What does Ruby have that Python doesn't, and vice versa?

You can have code in the class definition in both Ruby and Python. However, in Ruby you have a reference to the class (self). In Python you don't have a reference to the class, as the class isn't defined yet.

An example:

class Kaka
puts self
end

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.

Python vs. Ruby for metaprogramming

There's not really a huge difference between python and ruby at least at an ideological level. For the most part, they're just different flavors of the same thing. Thus, I would recommend seeing which one matches your programming style more.

python and ruby - for what to use it?

They are good for mostly for rapid prototyping, quick development, dynamic programs, web applications and scripts. They're general purpose languages, so you can use them for pretty much everything you want. You'll have smaller development times (compared to, say, Java or C++), but worse performance and less static error-checking.

You can also develop desktop apps on them, but there may be some minor complications on shipping (since you'll usually have to ship the interpreter too).

You shouldn't do critical code or heavy computations on them - if you need these things, make them on a faster language (like C) and make a binding for the code. I believe Python is better for this than Ruby, but I could be wrong. (OTOH, Ruby has a stronger metaprogramming)

What will a Python programmer gain by learning Ruby?

For the most part, nothing. Most of Ruby's strengths/weaknesses are the same as Python's, except that Ruby is slightly more "functional". However if you have Haskell as an option, much more can be learned about functional programming from Haskell than from Ruby.

Second, if you're looking at things from a theoretical computer science perspective, then Ruby is far from a language of choice. Ruby and a lot of its libraries break a lot of standard OOP dogma which I believe many academics would find repulsive (this is based mainly on my chats about languages with various professors).

From an academic perspective I think Haskell would have the most appeal to you. If you're interested in AI or logic, then Prolog is also an excellent choice.

Ruby vs. Python: which one is easier to keep track of different language versions?

As far as Python, most people are using the latest version of the 2 series. Python in general can be a nightmare as to mixing versions (at least, it used to be), but the solution is simply to deliver your software with the entire python environment it depends on.

Please advise on Ruby vs Python, for someone who likes LISP a lot

I'd go with Ruby. It's got all kinds of metaprogramming and duck punching hacks that make it really easy to extend. Features like blocks may not seem like much at first, but they make for some really clean syntax if you use them right. Open classes can be debugging hell if you screw them up, but if you're a responsible programmer, you can do things like 2.days.from_now (example from Rails) really easily (Python can do this too, I think, but with a bit more pain)

PS: Check out "Why Ruby is an acceptable LISP".

Why would one want to use ruby over python or vice versa?

I just came across this comparison, b/w Ruby and Python, which is in terms of performance and memory management.

A fair comparison can be found here. Further, I tend to agree with all the three answers above.

In plain English, what are the difference between Python, Perl, and Ruby when it comes to translating them to another language?

I understand what you asking, but that's not the right question (I'll get to that in a second).

Answer is because Perl is not defined by a formal language definition. Perl is somewhat in uncommon in that the reference for the language IS the implementation of the interpreter. Other languages such as Python, and C have a language definition. This definition is the reference and the compiler/interpreter is implemented to conform to that definition.

This reference allows someone to take the lexer/parser steps for one language and apply it in reverse to the syntax tree of another language.

ie. Python Lexer -> Python Parson ->
AST -> C Parser -> C Lexer

This allows you it easily create a program to transcribe Python to C. Of course it's not that easy, but you get the idea.

For Perl there is no reference other than the Perl interpreter. Because there is a reference, it is possible to create a program that will translate the code, HOWEVER it would be very difficult compared to a language that had their reference in BNF (for example).

The question you wanted to ask is:

Why is it so much more difficult to
automatically translate Perl to C and
vice-versa, than Python to C?



Related Topics



Leave a reply



Submit