Software Development
Łukasz Brzeszcz
2021-09-22

The Ultimate Breakdown: Ruby vs. Python

Ruby and Python are two great programming languages interpretation-and dynamic typing-wise. You can read about their applications, popularity and community in one of the entries on this blog. These languages have many things in common, but also many differences. In this article, I will introduce some of them.

Similarities

As these languages ​​can be included in the same category of dynamically typed interpreted languages, there is no doubt that they are similar.

Dynamic typing

Both Python and Ruby are dynamically typed languages. As a result, the programmer does not have to specify the type of variables while writing the code. Their type is determined while the program is running and it may change. The type of a variable is derived from the value stored in it. The following code will run correctly in these languages:

variable = 1
variable += 2.5
variable = 'sample string'
variable = [2, 3.5, 'sample string']

Pure syntax

This is related to the point above, inter alia. Due to the fact that there is no need to declare types, end statements with a semicolon, and also the fact that they are scripting languages, both writing and reading it is easy. Well-written code will be readable even by people who have not had any previous contact with these languages.

They have interactive console interpreters

When you want to perform a simple operation or test a piece of code, you don't need to create a file and run the code using a special command. Both Ruby and Python have interactive console interpreters (REPL). For Python, we can run such an interpreter with the command python (orpython3), while for Ruby it could be, for example, irb:

$ irb
2.5.8 :001 > result = 2 + 1
=> 3
2.5.8 :002 > result += 3.5
=> 6.5

Differences

As these are different programming languages, of course they must have differences (I know, what a revealing statement). In the case of Python and Ruby, these differences are numerous. I will describe some of the most significant ones in my opinion.

Indents

Indentation is very important in Python. All code blocks are defined by indentation. It is important that each line in a given block has the same indentation. Otherwise, when we try to run the code, we will get IndentationError. Ruby takes a different approach. Here, the code block is limited by keywords. We distinguish the beginning word (e.g.begin, if, class, def) and the ending word end. It doesn't matter how the code is indented inside the code block.

Inheritance

One of the basic features of object oriented programming. In the right hands, it can work wonders. Python supports multi-base inheritance:

example.py
class ClassA:
    def callA(self):
        print('callA')
class ClassB:
    def callB(self):
        print('callB')
class ClassAB(ClassA, ClassB):
    pass
class_inst = ClassAB()
class_inst.callA()
class_inst.callB()

As a result, we get:

$ python3 example.py
callA
callB

Ruby only supports single-base inheritance using the < operator by default. However, it is possible to simulate multi-base inheritance using modules:

example.rb
class ClassA
  def call_a
    puts 'call_a'
  end
end
 
module ModuleB
  def call_b
    puts 'call_b'
  end
end
 
class ClassAB < ClassA
  include ModuleB
end
 
class_inst = ClassAB.new
class_inst.call_a
class_inst.call_b

What gives us:

$ ruby example.rb
call_a
call_b

include is just one of the mixins available in Ruby. Mixins is a way to add extra functionality to classes. There are three mixins in Ruby.

Include

When we call include in class definition, included module becomes the direct ancestor of this class. It means that every method of module becomes instance method of this class. We can override them in class and also call original method (defined in module) using super keyword.

example.rb
module ModuleA
  def print_something
    puts 'Message from module'
  end
end
 
class ClassB
  include ModuleA
 
  def print_something
    puts 'Message from class'
    super
  end
end
 
ClassB.new.print_something

Result:

$ ruby example.rb
Message from class
Message from module

Prepend

It works almost like include, but prepended module becomes direct descendant of class. It means that we cannot override modules method in class, but method in module can call class method usingsuper keyword.

example.rb
module ModuleA
  def print_something
    puts 'Message from module'
    super
  end
end
 
class ClassB
  prepend ModuleA
 
  def print_something
    puts 'Message from class'
  end
end
 
ClassB.new.print_something

Result:

$ ruby example.rb
Message from module
Message from class

Extend

It works similar to include except that the methods defined in the module become class methods of the class.

Result:

$ ruby example.rb
Message from module

Functions and blocks

Python has functions. Ruby supports only methods. What does it entail? Among other things, Python can hold a function in a variable and pass it as an argument to another function. In Ruby, we can't do it that easily. This is also related by the parentheses. Given a function with no arguments in Python, or with default arguments, if you use its name without parentheses, the function will be returned. Only adding the parentheses leads to its execution. In Ruby, we can call functions without parentheses:

example.py 
def inner_function():
    print('Inner function')
 
def wrapper_function(function):
    print('Wrapper function')
    # function is a variable that contains function object
    function() # inner function is called here
 
wrapper_function(inner_function)

Result:

$ python3 example.py
Wrapper function
Inner function

In Ruby:

example.rb
def inner_function
  puts 'Inner function'
end
 
def wrapper_function(function)
  puts 'Wrapper function'
  function
end
 
wrapper_function(inner_function) # inner_function is called here
 

Result:

$ ruby example.rb
Inner function
Wrapper function

Of course, in this case, you can do some tricks in Ruby to achieve the desired effect. First, we can use the Proc object:

example.rb
def inner_function
  puts 'Inner function'
end
 
def wrapper_function(function)
  puts 'Wrapper function'
  function.call
end
 
func = Proc.new { inner_function }
wrapper_function(func)

Result:

$ ruby example.rb
Wrapper function
Inner function

The second approach is to use blocks:

example.rb
def inner_function
  puts 'Inner function'
end
 
def wrapper_function
  puts 'Wrapper function'
  yield
end
 
wrapper_function do
  inner_function
end

Result:

$ ruby example.rb
Wrapper function
Inner function

List comprehension

This is a very useful Python feature. It consists in creating a list based on another list. Let us assume that we have such a task: given a list of numbers, make a list of squares of odd numbers. In Python, we use list comprehension:

numbers = [1, 3, 4, 7, 8, 12, 15]
result = [num*num for num in numbers if num % 2]

The advantage is that we only have two lists in memory.

In Ruby, things are a bit different. There are several ways to solve this task. I will present the most readable of them:

numbers = [1, 3, 4, 7, 8, 12, 15]
result = []
numbers.each { |num| result << num * num if num.odd? }

As we can see, it can be done, but not quite as elegant as in Python.

All classes are mutable

In Ruby, all classes are mutable. What does it mean? We can add or override methods of all classes, even the built-in ones. Suppose we want a method that changes every letter an into b in the string. We can do it in a simple way:

example.rb
class String
  def a_to_b!
    self.gsub!(/a/, 'b')
  end
end
 
string = 'Example string with many a letters'
puts string
string.a_to_b!
puts string

Result:

$ ruby example.rb
Example string with many a letters
Exbmple string with mbny b letters

Summary

Ruby and Python are wonderful languages. They have their pros and cons. Python is a much more popular language, has a considerably larger community and is considered to be a language with a steep learning curve. It has a multitude of applications, including web application development, machine learning, big data. On the other hand, there is Ruby. A less popular language, intended mainly for writing web applications. Which language won this duel? It seems to me that there is no right answer to this question. Personally, I think Ruby is a language worth paying attention to as currently it is just underappreciated.

Product development consulting

Read More

GraphQL Ruby. What about performance?

Rails and Other Means of Transport

Rails Development with TMUX, Vim, Fzf + Ripgrep