In plain English, when you delegate something to someone else, you divide up responsibilities amongst yourselves.
Attach additional responsibilities to an object dynamically.
Packing a bunch of functionality into a single object’s class and instance methods.
class Product
def sku
# Returns a sku specific to a
# particular instance of a product.
end
end
class Book < Product
def author
# Returns an Author object
# associated with that book.
end
end
book = Book.new
book.sku #skuclass Report
def records
[1, 2, 3, 4, 5]
end
def print
p "Game of Thrones S07E07's script got leaked"
end
end
class InTriplicateReport < Report
def records
record = super
record * 3
end
def print
super
super
super
end
end
report = InTriplicateReport.new
p report.records
report.print
Any time you use another object to perform logic instead of keeping it within the class itself, you are delegating
by Active Support
require 'active_support/core_ext/module/delegation'
class LazyEmployee
def initialize(sandwich_maker)
@sandwich_maker = sandwich_maker
end
delegate :make_me_a_sandwich, to: :sandwich_maker
private
attr_reader :sandwich_maker
endby SimpleDelegator class
class User
def born_on
Date.new(1989, 9, 10)
end
end
class UserDecorator < SimpleDelegator
def birth_year
born_on.year
end
endA concrete implementation of Delegator, this class provides the means to delegate all supported method calls to the object passed into the constructor and even to change the object being delegated to at a later time with #__setobj__ - RubyDoc
The Forwardable module provides delegation of specified methods to a designated object, using the methods def_delegator and def_delegators.
class Book
def language
"English"
end
def year
"1926"
end
def author
"Ernest Hemingway"
end
def title
"The Sun Also Rises"
end
end
class Product
def initialize
@book = Book.new
end
def language
@book.language
end
def year
@book.year
end
def author
@book.author
end
def info
@book.title
end
end