Back

Switching from Ruby to Python - Syntax, scopes, decorators and generators

Published on May 09, 2020 15:15, written by Martina Šimičić

A short list of some of the concepts and differences if you are switching from Ruby to Python.

While Python and Ruby are at first quite similar, there are some important differences. So what is the simplest way to switch and quickly get up to speed? Well, lets see the approach for following categories:

Running different versions

Similar to rvm and rbenv for managing different versions of Ruby, Python has its alternatives. Amongs the sea of options, pipenv seems to be the best and most used. (Pipenv repo)

Syntax

It is quite similar but still different enough to have an affect on the speed of development. There are few things that set Python and Ruby apart, some of those are:

  1. Indentation
  2. Using : in the statements
  3. Working with arrays/lists
  4. For loops statements
  5. Loop controls
  6. Usage of the return statement
  • Indentation. While indentation in Ruby is not enforced but sign of a good practice, in Python it is really important to keep proper indentation (4 spaces)

  • If statements, loops, class definitions, function and method definitions always end in colon - :. For example:

# Ruby
if animal == 'dog' do 
  p "I am a dog!"
elsif animal == 'cat' do
  p "I am a cat!" 
else 
  p "I am just an animal..."
end


# Python
if animal == 'dog': 
    p "I am a dog!"
elif animal == 'cat':
    p "I am a cat!" 
else:
    p "I am just an animal..."

  • Working with array/list slicing
# Ruby
arr = ['A', 'D', 'F', 'Z', 'J']
arr[1..3]
=> ['D', 'F', 'Z']
# same way: arr[0..1] or arr[3..-1]


# Python 
arr = ['A', 'D', 'F', 'Z', 'J']
arr[1:4]
=> ['D', 'F', 'Z']
# same way: arr[:2] or arr[3:]

  • "For" loops
# Ruby
[1, 2, 3].each { |item| ... }

# Python
for item in [1, 2, 3]: ... 

  • Loop control
# Ruby
break         # exit the loop
next           # continue with the next iteration

# Python
break               # exit the loop
continue         # continue with the next iteration
  • Return statement is required. While in Ruby, last line/block is also the return of the function, in Python there has to be a return if the method needs to return something.

Data structures

Besides the data structures that are the same in Ruby and Python like array/list, hash/dictionary and sets, Python implements tuple. Tuple as such does not exist in Ruby, but one could use it with a little help of a Struct. Still, result will map more to named tuple than the anonymous one.

# Ruby
Animal = Struct.new(:name, :age)
animal = Animal.new
animal.name = "Rex"
animal.age = 2

# Python
("Rex", 2)

Variables scope

I found this to be the biggest surprise. While Ruby is quite strict with the scope, Python has a rules for lookup of the variable. When it come to Ruby we have global, instance, local and class variable defined as follows:

# Ruby 
$                 # global scope
@               # instance
[a-z]*         # local
@@           # class 

When it comes to Python, there is a LEGB rule for resolving names: - local scope - enclosed scope - global scope - built in For more info take a look at the official documentation.

Generators

Generator functions allow for generation of the values dynamically instead of generating them and storing in the memory. They will automatically suspend and resume their execution and state around the last point of value generation, which is a great way for improving performance, read more about the topic here.

Decorator functions

Because of the widespread use, we should not forget function decorators in Python, it allows for adding new functionality to an existing function without modifying its internals. See more about the decorator pattern here.

# Python 
@decorator_function
def function_to_be_decorated():
    ....

For Ruby, we can accomplish the same in different ways, here is the overview of some of the approaches: Decorators implementation - Ruby

Recap

These are all some of the basic differences between the languages that can help you with switching fast and stay productive.

Tags:

  • ruby
  • development
  • python