Skip to content

Instantly share code, notes, and snippets.

@robin850
Last active December 17, 2015 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robin850/5540550 to your computer and use it in GitHub Desktop.
Save robin850/5540550 to your computer and use it in GitHub Desktop.
Ruby vs Python (in my opinion)

Ruby Vs Python

Where Python > Ruby

  • Chained comparisons (3 < 5 < 7 => True) ; Exception in Ruby

Where Ruby > Python

  • puts "foo" if condition

  • IdentationError sucks!

  • predicate (and bang) methods with ? (and !) (e.g. : defined?)

  • No need to call most methods with ()

  • Operator overloading and "special" methods name clearer in Ruby:

      class Foo
        def +(n)
        end
    
        def [](el)
        end
      end
    
      foo + bar
      foo[:bar]
      class Foo():
        def __add__(self, n):
          # ...
        def __getitem__(self, el):
          # ....
    
      foo + bar
      foo[:bar]
  • "List comprehensions" (_can be simplier in Python in some cases)

    a = (0..9).to_a
    alphabet = ("a".."z").to_a
    l = [i for i in range(10)]
    alphabet = string.lowercase[26]
  • Nicer conversion methods (OO style):

      1.to_f # => 1.0
      [1, 2, 3].to_s # => "[1, 2, 3]"
    float(1) # => 1.0
    str([1, 2, 3]) # => "[1, 2, 3]"
  • Nicer blocks (+ lambdas/procs) e.g.:

      proc = -> (e) {
        puts "Current element : #{e}"
      }
    
      [1, 2, 3, 4].each(&proc)
  • Ability to manipulate basic objects such as String

      class String
        def foo
          "Let me introduce myself : #{self}"
        end
      end
    
      "Foo".foo # => "Let me introduce ..."
  • Implicit return statements

      def foo
        # Same as with a `return` statement
        "bar baz"
      end
    
      foo # => "bar baz"
      def foo():
        "bar baz"
      not foo() # => True
    
      def foo():
        return "bar baz"
      foo() # => "bar baz"
  • File manipulation (no example, I want to keep this gist clean!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment