forwardable_presentation



forwardable_presentation

0 0


forwardable_presentation


On Github MatthewRDodds / forwardable_presentation

Delegation in Ruby

The Forwardable Module

What is it?

The Forwardable module provides delegation of specified methods to a designated object, using the methods def_delegator and def_delegators.

-- Ruby Source

Example (Before & After)

What is the advantage of this?

Isolation of delegated object reference in code (DRY) Less verbosity, looks and feels cleaner

What can you delegate to?

  • Methods
  • Objects
  • Instance Variables

Delegation can make refactoring easier

Method can be moved and called the same way from the original class.

Summary

The Forwardable module offers some interesting ways to compose classes and sharing of responsibilities.

Credits

For subject information and examples:

http://vaidehijoshi.github.io/blog/2015/03/31/delegating-all-of-the-things-with-ruby-forwardable/

http://radar.oreilly.com/2014/02/delegation-patterns-in-ruby.html

For slidedeck structure:

https://github.com/JoelQ/rack_slides