The Forwardable module in Ruby — Part I

Tech - RubyCademy
RubyCademy
Published in
3 min readJul 16, 2018

In this article we’re going to explore the following topics:

  • the Forwardable module
  • the def_delegator method
  • the def_delegators method
  • the delegate method

The Forwardable module

Forwardable is a module that can be used to add behavior to all the instances of a given class.

This module is included in the singleton class using the extend keyword in order to add methods at the class level (to keep it simple).

So let’s break down the API.

The def_delegator method

Forwardable#def_delegator allows an object to forward a message to a defined receiver.

NB: feel free to have a look to my article if you’re unfamiliar with the notion of messages and receivers in Ruby.

Nothing better than an example to demystify the previous assertion

# in forwardable.rb
class Hero
attr :skills
def initialize
@skills = [:strong, :keen, :brave]
end
end
jack = Hero.newputs "Jack's main skill: #{jack.skills.first}"

produces

?> ruby forwardable.rb
Jack's main skill: strong

This works... But calling jack.skills.first outside of the classHero is a bit.. weird!

So let’s encapsulate this code into Hero

# in forwardable.rb
class Hero
attr :skills
def initialize
@skills = [:strong, :keen, :brave]
end
def main_skill
@skills.first
end
end
jack = Hero.newputs "Jack's main skill: #{jack.main_skill}"

produces

?> ruby forwardable.rb
Jack's main skill: strong

Better! Here Hero#main_skill contains the logic to access jack‘s main skill.

This solution is acceptable. But Ruby provides a mechanism to forward a message (#first) from an instance (jack) to an explicit receiver (skills) using the Forwardable#def_delegator method.

So let’s modify our gist using this method

# in forwardable.rb
require 'forwardable'
class Hero
attr :skills
extend Forwardable def_delegator :@skills, :first, :main_skill def initialize
@skills = [:strong, :keen, :brave]
end
end
jack = Hero.newputs "Jack's main skill: #{jack.main_skill}"

produces

?> ruby forwardable.rb
Jack's main skill: strong

Cool! Here we avoid creating a getter method to access skills.first by using the message forwarding system provided by Ruby.

First, we require the forwardable library.

Then, we extend Forwardable in order to add the module methods at the class level.

Then we use the freshly added class-level method def_delegator:

  • The first argument :@skills correspond to the receiver of the message forwarding.
  • The second argument :first is the message to forward.
  • And finally the third argument :main_skill is an alias of the :first message. So, when we call jack.main_skill — which is more readable than jack.first — then internally the skills.first will be automatically called.

The def_delegators method

The 2 main differences withdef_delegator is that it takes a set of methods to forward and the methods cannot be aliased

# in forwardable.rb
require 'forwardable'
class Todolist
attr :tasks
extend Forwardable def_delegators :@tasks, :first, :last def initialize
@tasks = %w[conception implementation refactoring]
end
end
todolist = Todolist.newputs "first tasks: #{todolist.first}"
puts "last tasks: #{todolist.last}"

produces

?> ruby forwardable.rb
first tasks: conception
last tasks: refactoring

Here, the first and last method of the tasks array are available for any instance of Todolist.

when one of these 2 methods is called then the message is forwarded to the tasks array.

The delegate method

delegate accepts a hash as an argument where:

  • the key is one or more messages
  • the value is the receiver of the messages defined as key
# in forwardable.rb
require 'forwardable'
class Computer
attr :cores, :screens
extend Forwardable delegate %I[size] => :@cores,
%I[length] => :@screens
def initialize
@cores = (1..8).to_a
@screens = [1, 2]
end
end
macrosoft = Computer.newputs "Cores: #{macrosoft.size}"
puts "Screens: #{macrosoft.length}"

produces

$> ruby forwardable.rb
Cores: 8
Screens: 2

Here, the macrosoft.size message corresponds to the macrosoft.cores.size.

And, the macrosoft.length message corresponds to the macrosoft.screens.length.

Feel free to have a look to the Part II about the SingleForwardable module.

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.

💚

--

--