Ruby is a Multi-paradigm programming language

Tech - RubyCademy
RubyCademy

--

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 at what Ruby proposes as an 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 at the Object-Oriented Programming paradigm and Ruby to give a concrete example of what’s a programming paradigm.

In essence, Ruby is associated with 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 or self
  • 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 with the Object-Oriented Programming paradigm.

Cool!

Now that we’re more familiar with what’s a programming paradigm, let’s have a look at a set of paradigms that can be associated with 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 into a function.

A function — in opposition to a method — is defined from 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 of classes

Here, procedure1 and procedure2 methods are defined from outside classes.

Then we call these 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.

Indeed, 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 are 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 the object passed as an 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 enemies of this programming paradigm.

Higher-order functions

Higher-Order functions are functions that can take a function as an argument or return a function

Here, the process method takes an object that responds to 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 an 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 lambdas and procs to elegantly simulate the behavior of a method passed as an 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 with 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 an argument.

The method doesn’t depend on the type of argument.

So this method is generic and can be used for any kind of comparable objects.

Conclusion

Ruby, is 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 a wide variety of applications.

Ruby Mastery

We’re currently finalizing our first online course: Ruby Mastery.

Join the list for an exclusive release alert! 🔔

🔗 Ruby Mastery by RubyCademy

Also, you can follow us on x.com as we’re very active on this platform. Indeed, we post elaborate code examples every day.

💚

--

--