In this article, we’re going to explore the following topics:
- what’s a paradigm?
- programming paradigms in Ruby
Even if Ruby is a fully Object-Oriented Programming language, it can also be interpreted through some other specific prisms.
So let’s have a look to what Ruby proposes as alternative to the Object-Oriented Programming paradigm.
What’s a paradigm?
In science and philosophy, a paradigm is a distinct set of concepts or thought patterns, including theories, research methods, postulates, and standards for what constitutes legitimate contributions to a field.
Let’s have a look to the Object-Oriented Programming paradigm and Ruby to give a concrete example of what’s a programming paradigm.
By essence, Ruby is associated to the Object-Oriented Programming paradigm.
This paradigm is composed of the following concepts:
- objects and attributes
- data encapsulation
- data access control
- data manipulation via
- class as type
- first-class objects
We can encounter all these concepts as features in Ruby.
These features are considered the backbone of the language.
That’s why Ruby is associated to the Object-Oriented Programming paradigm.
Now that we’re more familiar to what’s a programming paradigm, let’s have a look to a set of paradigms that can be associated to Ruby — depending on the features proposed by the language.
Programming paradigms in Ruby
Here, we’re going to talk about 3 programming paradigms that can be apprehended by Ruby:
- the Procedural programming paradigm
- the Functional programming paradigm
- the Generic programming paradigm
The Procedural programming paradigm
This paradigm is based on the concept of procedure calls.
In computer programming, a procedure is mainly assimilated to a function.
A function — in opposition to a method — is defined outside classes.
So, a procedure call can be seen as a function call.
In Ruby, functions don’t exist.
But we can create methods outsides classes
procedure2 methods are defined outsides classes.
Then we call theses methods procedurally.
In fact, these methods are still defined within an object: the
Feel free to read my article about the
Ruby Object Modelif you’re unfamiliar with the
mainobject in Ruby.
This way to use Ruby is popular among the devops community.
In effect, they often need to build scripts to interact with the systems they use.
The functional programming paradigm
In the OOP paradigm, everything turns around objects.
In the functional programming paradigm, everything turns around functions.
There is 3 main concepts around functional programming:
- Pure functions
- Higher-Order functions
- First-Class functions
A pure function is a function that always produces the same output for a given input
pure_method always returns the result of object passed as argument, times
For a given input, the output will always be the same.
By intent, the functional programming paradigm is strongly related to the concept of pure functions – as side effects are the enemy of this programming paradigm.
Higher-Order functions are functions that can take a function as argument or return a function
process method takes an object that responds to the
call method — a
Proc or a
lambda for example — and pass the
b arguments to
As it’s impossible to pass a method as argument of another method, then we use a
lambda to bypass this issue.
Feel free to have a look to my article about
Procs and Lambdasif you’re not familiar with lambdas in Ruby.
A programming language that proposes Higher-Order functions can be defined as a First-Class function programming language.
Ruby is a First-Class object programming language.
But we can use
procs to elegantly simulate the behaviour of a method passed as argument of another one.
Generic programming paradigm
Definition of Generic programming paradigm from Wikipedia:
Algorithms and methods are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters.
Ruby dynamic typing coupled to the Duck Typing design already provides all the necessary tools to apprehend Ruby as a generic programming language
gt method expects to receive 2 comparable objects as argument.
The method doesn’t depend on the arguments type.
So this method is generic and can be used for any kind of comparable objects.
Ruby, in a fully Object-Oriented programming language. But it also cherry-picks — and implements — concepts from other programming paradigms.
This approach allows Ruby developers to use this language in situations very far from each other.
ONE MORE THING ⬇
Feel free to subscribe here: www.rubycademy.com
Thank you for taking the time to read this post :-)
Feel free to 👏 and share this article if it has been useful for you. 🚀
Also, as I post an article every 3 days, feel free to follow me to be notified of my new article releases.
Here is a link to my last article: