class Delegator
This library provides three different ways to delegate method calls to an object. The easiest to use is SimpleDelegator. Pass an object to the constructor and all methods supported by the object will be delegated. This object can be changed later.
Going a step further, the top level DelegateClass method allows you to easily setup delegation through class inheritance. This is considerably more flexible and thus probably the most common use for this library.
Finally, if you need full control over the delegation scheme, you can inherit from the abstract class Delegator and customize as needed. (If you find yourself needing this control, have a look at Forwardable which is also in the standard library. It may suit your needs better.)
SimpleDelegator’s implementation serves as a nice example of the use of Delegator:
require 'delegate' class SimpleDelegator < Delegator def __getobj__ @delegate_sd_obj # return object we are delegating to, required end def __setobj__(obj) @delegate_sd_obj = obj # change delegation object, # a feature we're providing end end
Notes
Be advised, RDoc will not detect delegated methods.
Constants
- VERSION
-
The version string
Public Class Methods
(Symbol n) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 37
def self.const_missing: (Symbol n) -> untyped
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 39
def self.delegating_block: (Symbol mid) -> Proc
(untyped obj) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 160
def initialize: (untyped obj) -> void
Pass in the obj to delegate method calls to. All methods supported by obj will be delegated to.
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 41
def self.public_api: () -> untyped
Public Instance Methods
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 49
def !: () -> untyped
Delegates ! to the __getobj__
(untyped obj) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 57
def !=: (untyped obj) -> bool
Returns true if two objects are not considered of equal value.
(untyped obj) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 65
def ==: (untyped obj) -> bool
Returns true if two objects are considered of equal value.
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 74
def __getobj__: () -> untyped
This method must be overridden by subclasses and should return the object method calls are being delegated to.
(untyped obj) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 83
def __setobj__: (untyped obj) -> untyped
This method must be overridden by subclasses and change the object delegate to obj.
(untyped obj) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 91
def eql?: (untyped obj) -> bool
Returns true if two objects are considered of equal value.
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 99
def freeze: () -> self
:method: freeze Freeze both the object returned by __getobj__ and self.
(self obj, ?freeze: bool?) → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 162
def initialize_clone: (self obj, ?freeze: bool?) -> self
(self obj) → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 164
def initialize_dup: (self obj) -> self
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 107
def marshal_dump: () -> untyped
Serialization support for the object returned by __getobj__.
(untyped data) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 115
def marshal_load: (untyped data) -> void
Reinitializes delegation from a serialized object.
(Symbol m, *untyped args, **untyped) { (*untyped, **untyped) → untyped } → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 122
def method_missing: (Symbol m, *untyped args, **untyped) { (*untyped, **untyped) -> untyped } -> untyped
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 131
def methods: (?boolish all) -> Array[Symbol]
Returns the methods available to this delegate object as the union of this object’s and __getobj__ methods.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 140
def protected_methods: (?boolish all) -> Array[Symbol]
Returns the methods available to this delegate object as the union of this object’s and __getobj__ protected methods.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 149
def public_methods: (?untyped all) -> Array[Symbol]
Returns the methods available to this delegate object as the union of this object’s and __getobj__ public methods.
(Symbol m, bool include_private) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 173
def respond_to_missing?: (Symbol m, bool include_private) -> bool
Checks for a method provided by this the delegate object by forwarding the call through __getobj__.
(untyped target, Symbol m, bool include_private) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/delegate/0/delegator.rbs, line 181
def target_respond_to?: (untyped target, Symbol m, bool include_private) -> bool
Handle BasicObject instances