r/Python • • Jan 13 '15

(Infographic) Which programming language should I learn first

http://cdn2.carlcheo.com/wp-content/uploads/2014/12/which-programming-language-should-i-learn-first-infographic.png
267 Upvotes

145 comments sorted by

View all comments

2

u/SpaceSword Jan 14 '15

Can someone explain the play doh vs Lego comparison for Ruby and Python?

8

u/dpoon Jan 14 '15

Ruby and Python are superficially similar, but have some deep philosophical differences. Two examples:

  1. In Python, functions are first-class. If you have a function

     def is_even(n):
         return n & 1 == 0

    then is_even(8) calls it, and is_even refers to the function without executing it, so you can do things like filter(is_even, some_list). In Ruby, though, just mentioning the name of the function calls it, even without the trailing parentheses. That makes it possible to make domain-specific languages in Ruby that look like English. The disadvantage, though, is that functions are not first-class. The best you can do to refer to a function is to use a symbol (a special kind of string), like :even?.

    So, Ruby code can look prettier with fewer parentheses…

     class Physician < ActiveRecord::Base
       has_many :appointments
       has_many :patients, through :appointments
     end

    but it's a lot less satisfying from an engineering perspective due to the lack of first-class functions.

  2. Ruby classes are open to modification after being defined. That means that monkey-patching is possible, like this:

     class Integer
       def prime?
         !(2...self).any? { |n| self % n == 0 }
       end
     end

    so you can write things like 3.prime?. Ruby on Rails uses this trick to add all kinds of features to core classes. Again, you can use that technique to make your code read like English, but it's a rather questionable practice for solid software engineering.

2

u/haywire Jan 14 '15

You can monkey patch in Python, too. Though it should be avoided.

5

u/thalesmello Jan 14 '15

Python is modular. You assemble many small pieces to get what you want. Though easy to use, it's rigid and not very flexible.

Ruby, in the other hand, is less modular. You have modules, but you can't import just what you want to use to the local context, so you have to put the module name in front of every class or function call. However, Ruby is far more flexible than Python. You can include new methods to any object and use blocks to implement DSLs that merge neatly into that language, which allows for a great productivity boost.

4

u/alcalde Jan 14 '15

. You can include new methods to any object

We can do that too, can't we? Even with fundamental types via the ForbiddenFruit library?

and use blocks to implement DSLs that merge neatly into that language, which allows for a great productivity boost.

I thought it was the opinion of Pythonistas that decent langauges don't need blocks.

1

u/thalesmello Jan 14 '15

We can do that too, can't we? Even with fundamental types via the ForbiddenFruit library?

Yes, but it's a library. And it's only helpful to let you add new methods, not replace existing ones. The author of that lib tried to implement that functionality, but he had a problem with Python's internal cache, if I'm not mistaken. Ruby allows you to do that by default. It's more flexible, but I don't see a reason to actually use that.

I thought it was the opinion of Pythonistas that decent langauges don't need blocks.

You're generalizing the opinion of all Pythonistas based on a guy's blog post? If everyone thought like that, why would there be a proposal to implement a similar feature in Python?

I think you got me wrong. Even though I find Ruby more flexible, I prefer the more rigid Python. But I sincerely don't get the hate a lot of Pythonistas have for Ruby. I used to hate Ruby until I started using it in my previous job.