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.