End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Forwardable#def_instance_delegator
def_instance_delegator(accessor, method, ali = method)
lib/forwardable.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.2.0/lib/forwardable.rb, line 188
def def_instance_delegator(accessor, method, ali = method)
gen = Forwardable._delegator_method(self, accessor, method, ali)
# If it's not a class or module, it's an instance
mod = Module === self ? self : singleton_class
ret = mod.module_eval(&gen)
mod.__send__(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7'
ret
end
Define method as delegator instance method with an optional alias name ali. Method calls to ali will be delegated to accessor.method. accessor should be a method name, instance variable name, or constant name. Use the full path to the constant if providing the constant name. Returns the name of the method defined.
Example
class MyQueue CONST = 1 extend Forwardable attr_reader :queue def initialize @queue = [] end def_delegator :@queue, :push, :mypush def_delegator 'MyQueue::CONST', :to_i end q = MyQueue.new q.mypush 42 q.queue #=> [42] q.push 23 #=> NoMethodError q.to_i #=> 1
See also
#def_delegators
#def_instance_delegators
#def_delegator
#delegate
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0