A hidden gem for developers since Ruby 2.7: New shorthand syntax for argument forwarding

Viral Patel
2 min readNov 4, 2021
Message forwarding between Ruby Objects

Since Ruby 2.7 there is a new shorthand syntax and much of the developer’s delight, it is for forwarding arguments to other methods. It is one of the most common approaches used in Object-oriented programming. Forwarding messages to other objects.

Forwarding arguments to another object is used in various object-oriented design patterns, For example:

Before Ruby 2.7 was released, the most common way to forward arguments to other methods was as follows:

class Sender
def self.call(*args, **kws, &block)
Receiver.call(args, kws, &block)
end
end

It can be confusing and hard to read at times when the receiver is a mixed bag of different types of arguments, i.e. normal arguments, keyword arguments or sending a blockforward and there is an effort that goes into deciding how to de-structure arguments to forward them to other objects.

New syntax ...three dot notation, allows developers to simply call the receiver object without worrying about the structure of the arguments. This is great since it allows the Sender object to know less about the Receiver object which promotes loose coupling between two objects so you can change the Receiver object interface without worrying about how caller objects are calling them. This is how you do it,

class Sender
def self.call(...)
Receiver.call(...)
end
end

Enjoy this goodie and add it to your object-oriented programming toolkit.

--

--