Go to content
The Codest
  • About Us
  • Services
  • Our Team
  • Case studies
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
  • About Us
  • Services
  • Our Team
  • Case studies
    • Blog
    • Meetups
    • Webinars
    • Resources
Careers Get in touch
2021-09-22
Software Development

The Ultimate Breakdown: Ruby vs. Python

Łukasz Brzeszcz

The Ultimate Breakdown: Ruby vs. Python  - Image

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.

Read More

GraphQL Ruby. What about performance?

Rails and Other Means of Transport

Rails Development with TMUX, Vim, Fzf + Ripgrep

Related articles

Software Development

3 Useful HTML Tags You Might Not Know Even Existed

Nowadays, accessibility (A11y) is crucial on all stages of building custom software products. Starting from the UX/UI design part, it trespasses into advanced levels of building features in code. It provides tons of benefits for...

Jacek Ludzik
Software Development

5 examples of Ruby’s best usage

Have you ever wondered what we can do with Ruby? Well, the sky is probably the limit, but we are happy to talk about some more or less known cases where we can use this powerful language. Let me give you some examples.

Pawel Muszynski
Software Development

Maintaining a Project in PHP: 5 Mistakes to Avoid

More than one article has been written about the mistakes made during the process of running a project, but rarely does one look at the project requirements and manage the risks given the technology chosen.

Sebastian Luczak
Software Development

5 reasons why you will find qualified Ruby developers in Poland

Real Ruby professionals are rare birds on the market. Ruby is not the most popular technology, so companies often struggle with the problem of finding developers who have both high-level skills and deep experience; oh, and by the...

Jakub
Software Development

9 Mistakes to Avoid While Programming in Java

What mistakes should be avoided while programming in Java? In the following piece we answers this question.

Rafal Sawicki
Software Development

A Deeper Look at the Most Popular React Hooks

In the course of many interviews, I noticed that even experienced programmers have a problem with distinguishing Hooks, not to mention their more advanced capabilities. So, I will try to explain in this article how Hooks should...

Pawel Rybczynski

Subscribe to our knowledge base and stay up to date on the expertise from industry.

About us

Tech company specializing in scaling tech teams for clients and partners thanks to top-class development engineers.

    United Kingdom - Headquarters

  • Office 303B, 182-184 High Street North E6 2JA London, England

    Poland - Local Tech Hubs

  • Business Link High5ive, Pawia 9, 31-154 Kraków, Poland
  • Brain Embassy, Konstruktorska 11, 02-673 Warsaw, Poland
  • Aleja Grunwaldzka 472B, 80-309 Gdańsk, Poland

    The Codest

  • Home
  • About us
  • Services
  • Case studies
  • Know how
  • Careers

    Services

  • PHP development
  • Java development
  • Python development
  • Ruby on Rails development
  • React Developers
  • Vue Developers
  • TypeScript Developers
  • DevOps
  • QA Engineers

    Resources

  • What are top CTOs and CIOs Challenges? [2022 updated]
  • Facts and Myths about Cooperating with External Software Development Partner
  • From the USA to Europe: Why do American startups decide to relocate to Europe
  • Privacy policy
  • Website terms of use

Copyright © 2022 by The Codest. All rights reserved.

We use cookies on the site for marketing, analytical and statistical purposes. By continuing to use, without changing your privacy settings, our site, you consent to the storage of cookies in your browser. You can always change the cookie settings in your browser. You can find more information in our Privacy Policy.