Ruby is a Multi-paradigm programming language
source link: https://www.tuicool.com/articles/hit/fIj2An7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
In this article, we’re going to explore the following topics:
- what’s a paradigm?
- programming paradigms in Ruby
Introduction
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?
From Wikipedia:
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
- methods
- data manipulation via
this
orself
- class as type
- first-class objects
- etc..
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.
Cool!
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
Here, the procedure1
and procedure2
methods are defined outsides classes.
Then we call theses methods procedurally.
In fact, these methods are still defined within an object: the main
object.
Feel free to read my article about the Ruby Object Model
if you’re unfamiliar with the main
object 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
Pure functions
A pure function is a function that always produces the same output for a given input
Here, pure_method
always returns the result of object passed as argument, times 2
.
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
Higher-Orderfunctions are functions that can take a function as argument or return a function
Here, the process
method takes an object that responds to the call
method — a Proc
or a lambda
for example — and pass the a
and b
arguments to call
.
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 Lambdas
if you’re not familiar with lambdas in Ruby.
First-Class functions
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 lambda
s and proc
s 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
Here, the 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.
Conclusion
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.
Voilà!
ONE MORE THING ⬇
Feel free to subscribe here: www.rubycademy.com
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK