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 calla
puts 'calla'
end
end
module ModuleB
def callb
puts 'callb'
end
end
class ClassAB < ClassA
include ModuleB
end
classinst = ClassAB.new
classinst.calla
classinst.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
wrapperfunction(innerfunction)
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
wrapperfunction(innerfunction) # 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 { innerfunction }
wrapperfunction(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
wrapperfunction do
innerfunction
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 atob!
self.gsub!(/a/, 'b')
end
end
string = 'Example string with many a letters'
puts string
string.atob!
puts string
Result:
$ ruby example.rb
Example string with many a letters
Exbmple string with mbny b letters
Summary
Web development has been a dynamic field that continually adopts and shapes various programming languages to suit its needs. Two popular choices in this area include the Python programming language and Ruby. Both have proved their worth in building web applications and other forms of software, each having its unique benefits and drawbacks.
Python is a high-level programming language revered for its simplicity and code readability, a feature that often appeals to beginner programmers. The Python programming language is versatile and can be used for a variety of tasks. This general-purpose programming language is often used in areas like machine learning, big data, and, notably, web development. A broad community of Python developers contributes to its rich ecosystem of libraries and frameworks, enhancing its suitability for building web applications. Django, Flask, and Pyramid are among the various Python web frameworks leveraged by web developers around the globe.
On the other hand, Ruby, another high-level, general-purpose programming language, shines with its flexibility and expressiveness. This object-oriented programming language is the backbone of many web applications. Ruby’s charm lies in its ability to facilitate web development through the Rails framework, a powerful tool for building web applications rapidly.
Ruby, though less popular than Python, boasts an active and dedicated Ruby community that continually develops and maintains an assortment of libraries and tools, thereby enriching the experience of web developers using this language. As an object-oriented language, Ruby is prized for its elegance and the clean, readable code it produces, making it a fitting choice for both small and large scale web applications.
Python and Ruby, as object-oriented programming languages, share many similarities. They also support functional programming, offering programmers various styles to code in. Python code, in particular, is appreciated for its straightforward syntax, which underscores the language’s emphasis on simplicity and readability.
Each language has its strengths; Python’s versatility and wide community support make it an excellent tool for various tasks, while Ruby’s expressiveness and the Rails framework makes it a strong contender for web app development. It is the unique needs and preferences of web developers that will determine which language best serves their project.
In the realm of operating systems, both languages display an impressive level of portability, further enhancing their suitability for a multitude of tasks, not limited to web development. Therefore, it is challenging to declare a definitive “winner” in this duel.
While the Python programming language has a broader reach and larger community, Ruby’s capabilities shouldn’t be underestimated. To fully appreciate these languages, one must delve into them, understand their intricacies, and apply them where they shine the most. Whether you’re creating web applications with Python or Ruby, each language offers a robust platform to deliver high-quality web apps. Ruby, though less known, certainly deserves its due appreciation in the world of programming languages.