SimpleDelegator
SimpleDelegator is a class in Ruby’s standard library used to create lightweight decorator objects that delegate most method calls to another object. It is part of the Delegator framework, and SimpleDelegator inherits from Delegator to provide a straightforward way to wrap an object while allowing selective augmentation or override of behavior.
How it works: when you initialize a SimpleDelegator with a target object, method calls that are not
Usage typically involves requiring the delegate library and wrapping an object. For example, you can wrap an
class MyWrapper < SimpleDelegator
"Wrapper for: #{__getobj__.inspect}"
wrap = MyWrapper.new([1, 2, 3])
wrap.summary # => "Wrapper for: [1, 2, 3]"
wrap.length # => 3 (delegates to the wrapped array)
Relation to Delegator: SimpleDelegator provides a convenient base class for simple decorators, eliminating boilerplate while preserving