U:RDoc::NormalModule[iI"Forwardable:EF@0o:RDoc::Markup::Document:@parts[o;;[ o:RDoc::Markup::Paragraph;[I"=The Forwardable module provides delegation of specified ;TI"Fmethods to a designated object, using the methods #def_delegator ;TI"and #def_delegators.;To:RDoc::Markup::BlankLine o; ;[ I">For example, say you have a class RecordCollection which ;TI"Ocontains an array @records. You could provide the lookup method ;TI"G#record_number(), which simply calls #[] on the @records ;TI"array, like this:;T@o:RDoc::Markup::Verbatim;[I"require 'forwardable'
;TI"
;TI"class RecordCollection
;TI" attr_accessor :records
;TI" extend Forwardable
;TI"4 def_delegator :@records, :[], :record_number
;TI" end
;T:@format0o; ;[I"*We can use the lookup method like so:;T@o;;[I"r = RecordCollection.new
;TI"r.records = [4,5,6]
;TI" r.record_number(0) # => 4
;T;0o; ;[I"GFurther, if you wish to provide the methods #size, #<<, and #map, ;TI"Ball of which delegate to @records, this is how you can do it:;T@o;;[I"=class RecordCollection # re-open RecordCollection class
;TI"2 def_delegators :@records, :size, :<<, :map
;TI" end
;TI"
;TI"r = RecordCollection.new
;TI"r.records = [1,2,3]
;TI"!r.record_number(0) # => 1
;TI"!r.size # => 3
;TI",r << 4 # => [1, 2, 3, 4]
;TI",r.map { |x| x * 2 } # => [2, 4, 6, 8]
;T;0o; ;[I":You can even extend regular objects with Forwardable.;T@o;;[ I"my_hash = Hash.new
;TI"Mmy_hash.extend Forwardable # prepare object for delegation
;TI"Pmy_hash.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
;TI"my_hash.puts "Howdy!"
;T;0S:RDoc::Markup::Heading:
leveli: textI"Another example;T@o; ;[ I"SWe want to rely on what has come before obviously, but with delegation we can ;TI"Qtake just the methods we need and even rename them as appropriate. In many ;TI"Lcases this is preferable to inheritance, which gives us the entire old ;TI"0interface, even if much of it isn't needed.;T@o;;[!I"class Queue
;TI" extend Forwardable
;TI"
;TI" def initialize
;TI"/ @q = [ ] # prepare delegate object
;TI" end
;TI"
;TI"7 # setup preferred interface, enq() and deq()...
;TI"& def_delegator :@q, :push, :enq
;TI"' def_delegator :@q, :shift, :deq
;TI"
;TI"A # support some general Array methods that fit Queues well
;TI"@ def_delegators :@q, :clear, :first, :push, :shift, :size
;TI" end
;TI"
;TI"q = Queue.new
;TI"q.enq 1, 2, 3, 4, 5
;TI"q.push 6
;TI"
;TI"q.shift # => 1
;TI"while q.size > 0
;TI" puts q.deq
;TI" end
;TI"
;TI"$q.enq "Ruby", "Perl", "Python"
;TI"puts q.first
;TI"
q.clear
;TI"puts q.first
;T;0o; ;[I"This should output:;T@o;;[I"2
;TI"3
;TI"4
;TI"5
;TI"6
;TI"
Ruby
;TI" nil
;T;0S;
;i;I"
Notes;T@o; ;[I"8Be advised, RDoc will not detect delegated methods.;T@o; ;[I"R+forwardable.rb+ provides single-method delegation via the def_delegator and ;TI"Ndef_delegators methods. For full-class delegation via DelegateClass, see ;TI"+delegate.rb+.;T:
@fileI"lib/forwardable.rb;T:0@omit_headings_from_table_of_contents_below0;0;0[[
I"
debug;TI"RW;T:publicTI"lib/forwardable.rb;T[U:RDoc::Constant[i I"FORWARDABLE_VERSION;FI"%Forwardable::FORWARDABLE_VERSION;T00o;;[o; ;[I" Version of +forwardable.rb+;T;@~;0@~@cRDoc::NormalModule0[ [[I"
class;T[[;[ [:protected[ [:private[ [I"
instance;T[[;[[I"def_delegator;F@~[I"def_delegators;F@~[I"def_instance_delegator;F@~[I"def_instance_delegators;F@~[I"
delegate;F@~[I"instance_delegate;F@~[;[ [;[ [ [U:RDoc::Context::Section[i 0o;;[ ;0;0[@~I"lib/forwardable/impl.rb;T@±cRDoc::TopLevel