Slide 52
Slide 52 text
Ruby Conf Taiwan 2023
Forwardable
• def_delegators, delegate methods to
another object.
轉發呼叫給其他物件。
class RecordCollection
def_delegators :@records, :size,
:<<, :map
end # => [:size, :<<, :map]
r = RecordCollection.new # =>
#
r.records = [1,2,3] # => [1, 2, 3]
r.size # => 3
r << 4 # => [1, 2, 3, 4]
r.map { |x| x * 2 } # => [2, 4, 6,
8]