Method: Forwardable#def_instance_delegator
- Defined in:
- lib/forwardable.rb
#def_instance_delegator(accessor, method, ali = method) ⇒ Object Also known as: def_delegator
Define method as delegator instance method with an optional alias name ali. Method calls to ali will be delegated to accessor.method.
class MyQueue
extend Forwardable
attr_reader :queue
def initialize
@queue = []
end
def_delegator :@queue, :push, :mypush
end
q = MyQueue.new
q.mypush 42
q.queue #=> [42]
q.push 23 #=> NoMethodError
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/forwardable.rb', line 179 def def_instance_delegator(accessor, method, ali = method) line_no = __LINE__; str = %{ def #{ali}(*args, &block) begin #{accessor}.__send__(:#{method}, *args, &block) rescue Exception [email protected]_if{|s| Forwardable::FILE_REGEXP =~ s} unless Forwardable::debug ::Kernel::raise end end } # If it's not a class or module, it's an instance begin module_eval(str, __FILE__, line_no) rescue instance_eval(str, __FILE__, line_no) end end |