Ruby is my favorite programming language but I find Python to be more in demand these days (maybe because Python is still the most common language taught at universities?). Luckily, when you’re learning a new language, you don’t have to start from scratch—no need to re-learn what a verb or noun is… you just have to learn some vocabulary and the particular way the foreign language puts words together to make meaningful statements.
Similarly, the basic building blocks of programming, like data structures, scope, and flow control, are found across all programming languages. Most often, you really only need to learn new syntax and vocabulary. So if you need to make the switch from Ruby to Python, I began compiling some of their differences…
Defining methods
Python starts methods with a colon. Ruby does not.
Ruby closes methods with an //--ruby waydef simple_method
number = 5
apple = 7
endoutput>> 7def simple_method
number = 5
apple = 7
return number
endoutput>> 5//--python waydef simpleMethod:
number = 5
apple = 7output>> Nonedef simpleMethod:
number = 5
apple = 7
passoutput>> Nonedef simpleMethod:
number = 5
apple = 7
return 7output>> 72 keyword. Python does not.
task1
task2
end //--python waydef methodName(args):
task1
task2
Return Values
In Ruby, methods implicitly (read: magically) returns the value of the last line of the expression unless an explicit //--ruby waydef simple_method
number = 5
apple = 7
endoutput>> 7def simple_method
number = 5
apple = 7
return number
endoutput>> 5//--python waydef simpleMethod:
number = 5
apple = 7output>> Nonedef simpleMethod:
number = 5
apple = 7
passoutput>> Nonedef simpleMethod:
number = 5
apple = 7
return 7output>> 73 statement was issued.
In Python, methods must end with //--ruby waydef simple_method
number = 5
apple = 7
endoutput>> 7def simple_method
number = 5
apple = 7
return number
endoutput>> 5//--python waydef simpleMethod:
number = 5
apple = 7output>> Nonedef simpleMethod:
number = 5
apple = 7
passoutput>> Nonedef simpleMethod:
number = 5
apple = 7
return 7output>> 73 or //--ruby waydef simple_method
number = 5
apple = 7
endoutput>> 7def simple_method
number = 5
apple = 7
return number
endoutput>> 5//--python waydef simpleMethod:
number = 5
apple = 7output>> Nonedef simpleMethod:
number = 5
apple = 7
passoutput>> Nonedef simpleMethod:
number = 5
apple = 7
return 7output>> 75 or it will fall back to an implicit return statement that returns //--ruby waydef simple_method
number = 5
apple = 7
endoutput>> 7def simple_method
number = 5
apple = 7
return number
endoutput>> 5//--python waydef simpleMethod:
number = 5
apple = 7output>> Nonedef simpleMethod:
number = 5
apple = 7
passoutput>> Nonedef simpleMethod:
number = 5
apple = 7
return 7output>> 76. You gotta be more explicit.
number = 5
apple = 7
endoutput>> 7def simple_method
number = 5
apple = 7
return number
endoutput>> 5//--python waydef simpleMethod:
number = 5
apple = 7output>> Nonedef simpleMethod:
number = 5
apple = 7
passoutput>> Nonedef simpleMethod:
number = 5
apple = 7
return 7output>> 7
Indentation
Ruby is indentation-insensitive. It only looks for line breaks and //--ruby waydef simple_method
number = 5
apple = 7
endoutput>> 7def simple_method
number = 5
apple = 7
return number
endoutput>> 5//--python waydef simpleMethod:
number = 5
apple = 7output>> Nonedef simpleMethod:
number = 5
apple = 7
passoutput>> Nonedef simpleMethod:
number = 5
apple = 7
return 7output>> 72 to distinguish one code block from another. However, indentation is useful for readability, so use it anyway! Python, on the other hand, requires indentation. Unless you write your entire code block on one line or use continuation lines, indentation levels indicate which block is nested in which. The absolute amount of indentation doesn’t matter as much as indentation a block has relative to other blocks. Python thinks it has reached the end of a function once the indentation level falls back to the level where the function is defined.
task1
if conditional
task2
end
enddef methodB(args)
task1
if conditional
task2
end
end//--python waydef methodB(args):
task1
if conditional
task2
Inheritance
Ruby uses //--ruby waydef simple_method
number = 5
apple = 7
endoutput>> 7def simple_method
number = 5
apple = 7
return number
endoutput>> 5//--python waydef simpleMethod:
number = 5
apple = 7output>> Nonedef simpleMethod:
number = 5
apple = 7
passoutput>> Nonedef simpleMethod:
number = 5
apple = 7
return 7output>> 78 to signify a child inheriting from a parent class. Python places the parent class inside parentheses as if passing an argument to the child.
String Interpolation
Ruby sees everything as an expression that can be evaluated to a string then passed to a //--ruby waydef simple_method
number = 5
apple = 7
endoutput>> 7def simple_method
number = 5
apple = 7
return number
endoutput>> 5//--python waydef simpleMethod:
number = 5
apple = 7output>> Nonedef simpleMethod:
number = 5
apple = 7
passoutput>> Nonedef simpleMethod:
number = 5
apple = 7
return 7output>> 79 or //--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
enddef methodB(args)
task1
if conditional
task2
end
end//--python waydef methodB(args):
task1
if conditional
task20 operator to print the string onscreen. Ruby uses //--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
enddef methodB(args)
task1
if conditional
task2
end
end//--python waydef methodB(args):
task1
if conditional
task21 to indicate string substitutions within an expression. Python distinguishes between statements and expressions but only expressions can be evaluated and therefore printed to the screen. Python uses //--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
enddef methodB(args)
task1
if conditional
task2
end
end//--python waydef methodB(args):
task1
if conditional
task22 or //--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
enddef methodB(args)
task1
if conditional
task2
end
end//--python waydef methodB(args):
task1
if conditional
task23 to indicate substitutions. To output a string, “Kim! My name is Slim Shady.”:
name2 = "Slim Shady"
puts "#{name1}! My name is #{name2}."//--python way 1print "{A}! My name is {B}.format("Kim", “Slim Shady”)//--python way 2name1 = "Slim Shady"
print "{}! My name is {}.format("Kim", name1)//--python way 3print "%s! My name is %s." % ("Kim", "Slim Shady")
Multiline Strings
Ruby uses heredoc //--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
enddef methodB(args)
task1
if conditional
task2
end
end//--python waydef methodB(args):
task1
if conditional
task24, whereas Python uses triple quotes //--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
enddef methodB(args)
task1
if conditional
task2
end
end//--python waydef methodB(args):
task1
if conditional
task25 or continuation lines //--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
enddef methodB(args)
task1
if conditional
task2
end
end//--python waydef methodB(args):
task1
if conditional
task26.
multiline string
multiline string
multiline string
HEREDOC//--python way 1text = """ multiline string
multiline string
multiline string """//--python way 2text = "multiline string"\
"multiline string"\
"multiline string"
Class Methods
Ruby references the calling object using self.method whereas Python passes the class instance to itself as an argument.
//--ruby waydef self.methodend//--python waydef method(self):
Arguments
Ruby distinguishes between optional and non-optional arguments. Python distinguishes between positional arguments and keyword arguments. As in Ruby, arguments to be passed in the same order as in the function signature UNLESS they are keyword arguments. Keywords can be passed in any order.
//--ruby wayarg1 = "I"arg2 = "love"def method(arg1, arg2, arg3="baloney")
puts "#{arg1} #{arg2} #{arg3}"
endoutput>> "I love baloney"//--python wayarg1 = "I"
arg2 = "love"def method(arg1, arg2, arg3="baloney"):
print "%s %s %s" % arg1, arg2, arg3output>> "I love baloney"
Note: Use %s to denote a string, %d to denote an integer, %f to denote float.
Instantiation
//--ruby way@burrito = Burrito.new//--python wayburrito = Burrito()Iteration
//--ruby waybags.each do |bag|//--python wayfor item in bags: (indent) i += 1Data types
Python automatically supports tuples. Ruby requires building a custom data type to support tuples, which is basically an array with two elements.
Ruby uses //--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
enddef methodB(args)
task1
if conditional
task2
end
end//--python waydef methodB(args):
task1
if conditional
task27 or //--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
enddef methodB(args)
task1
if conditional
task2
end
end//--python waydef methodB(args):
task1
if conditional
task28 where Python would use //--ruby waydef simple_method
number = 5
apple = 7
endoutput>> 7def simple_method
number = 5
apple = 7
return number
endoutput>> 5//--python waydef simpleMethod:
number = 5
apple = 7output>> Nonedef simpleMethod:
number = 5
apple = 7
passoutput>> Nonedef simpleMethod:
number = 5
apple = 7
return 7output>> 76.
For boolean data types, Ruby uses //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)0 or //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)1, whereas Python uses capitalized //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)2 or //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)3. Ruby’s //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)0 and //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)1 are instances of a //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)6 and a //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)7 respectively.
Dependencies
//--ruby waydef simple_methodnumber = 5
apple = 7
endoutput>> 7def simple_method
number = 5
apple = 7
return number
endoutput>> 5//--python waydef simpleMethod:
number = 5
apple = 7output>> Nonedef simpleMethod:
number = 5
apple = 7
passoutput>> Nonedef simpleMethod:
number = 5
apple = 7
return 7output>> 70
Standard Library
Length
//--ruby waydef simple_methodnumber = 5
apple = 7
endoutput>> 7def simple_method
number = 5
apple = 7
return number
endoutput>> 5//--python waydef simpleMethod:
number = 5
apple = 7output>> Nonedef simpleMethod:
number = 5
apple = 7
passoutput>> Nonedef simpleMethod:
number = 5
apple = 7
return 7output>> 71
Note: Python2 rounds down to an integer. Python3 defaults to float
In Ruby, //--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
enddef methodB(args)
task1
if conditional
task2
end
end//--python waydef methodB(args):
task1
if conditional
task20 does not add a new line. Use puts to add a new line //--ruby wayclass Dog < Animal//--python wayclass Dog(Animal)9 to the end of each argument is there isn’t one already. In Python //--ruby way: method A and method B are functionally the samedef methodA(args)
task1
if conditional
task2
end
enddef methodB(args)
task1
if conditional
task2
end
end//--python waydef methodB(args):
task1
if conditional
task20 adds a newline. Use //--ruby way name1 = "Kim"
name2 = "Slim Shady"
puts "#{name1}! My name is #{name2}."//--python way 1print "{A}! My name is {B}.format("Kim", “Slim Shady”)//--python way 2name1 = "Slim Shady"
print "{}! My name is {}.format("Kim", name1)//--python way 3print "%s! My name is %s." % ("Kim", "Slim Shady")1 to prevent adding a newline.