Module

Forwardable

The Forwardable module provides delegation of specified methods to a designated object, using the methods #def_delegator and #def_delegators.

For example, say you have a class RecordCollection which contains an array @records. You could provide the lookup method #record_number(), which simply calls #[] on the @records array, like this:

class RecordCollection
  extend Forwardable
  def_delegator :@records, :[], :record_number
end

Further, if you wish to provide the methods #size, #<<, and #map, all of which delegate to @records, this is how you can do it:

class RecordCollection
  # extend Forwardable, but we did that above
  def_delegators :@records, :size, :<<, :map
end

Also see the example at forwardable.rb.

Public Attributes
debug force Forwardable to show up in stack backtraces of delegated methods
Public Methods
def_delegator Alias for #def_instance_delegator
def_delegators Alias for #def_instance_delegators
def_instance_delegator 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.
def_instance_delegators Shortcut for defining multiple delegator methods, but with no provision for using a different name. The following two code samples have the same effect:
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.