module

Forwardable

v1_8_6_287 - Show latest stable

The <a href="/ruby/Forwardable">Forwardable</a> 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.

Attributes

[RW]debug

Files

  • lib/forwardable.rb