Aliasing methods when a module is included

Aliasing methods when a module is included

Share it now. It takes only a few seconds! 😉


Aliasing methods when a module is included

"abcd".reverse # => "dcba"
"abcd".invert  # => NoMethodError

module Reversable
  # klass == String here
  def self.included(klass) = klass.alias_method(:invert, :reverse)
end  

When the Reversable module is included in a class, then the invert alias is automatically added to the including class through the included hook method

String.include Reversable # triggers a call to Reversable.included(String)

"abcd".reverse # => "dcba"
"abcd".invert  # => "dcba"

We're also available on