Module | SingleForwardable |
In: |
lib/forwardable.rb
|
The SingleForwardable module provides delegation of specified methods to a designated object, using the methods def_delegator and def_delegators. This module is similar to Forwardable, but it works on objects themselves, instead of their defining classes.
Also see the example at forwardable.rb.
Defines a method method which delegates to obj (i.e. it calls the method of the same name in obj). If new_name is provided, it is used as the name for the delegate method.
See the example at forwardable.rb.
# File lib/forwardable.rb, line 200 200: def def_singleton_delegator(accessor, method, ali = method) 201: accessor = accessor.id2name if accessor.kind_of?(Integer) 202: method = method.id2name if method.kind_of?(Integer) 203: ali = ali.id2name if ali.kind_of?(Integer) 204: 205: instance_eval("def \#{ali}(*args, &block)\nbegin\n\#{accessor}.__send__(:\#{method}, *args,&block)\nrescue Exception\n$@.delete_if{|s| /^\\\\(__FORWARDABLE__\\\\):/ =~ s} unless Forwardable::debug\nKernel::raise\nend\nend\n", "(__FORWARDABLE__)", 1) 206: end
Shortcut for defining multiple delegator methods, but with no provision for using a different name. The following two code samples have the same effect:
single_forwardable.def_delegators :@records, :size, :<<, :map single_forwardable.def_delegator :@records, :size single_forwardable.def_delegator :@records, :<< single_forwardable.def_delegator :@records, :map
See the example at forwardable.rb.
# File lib/forwardable.rb, line 187 187: def def_singleton_delegators(accessor, *methods) 188: for method in methods 189: def_singleton_delegator(accessor, method) 190: end 191: end