D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Forwardable
/
Filename :
cdesc-Forwardable.ri
back
Copy
U:RDoc::NormalModule[iI"Forwardable:ET@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 <tt>@records</tt>. You could provide the lookup method ;TI"G#record_number(), which simply calls #[] on the <tt>@records</tt> ;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"UYou could use Forwardable as an alternative to inheritance, when you don't want ;TI"Uto inherit all methods from the superclass. For instance, here is how you might ;TI"Dadd a range of +Array+ instance methods to a new class +Queue+:;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 = Thread::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_below0o;;[ ;I"lib/forwardable/impl.rb;T;0;0;0[[ I" debug;TI"RW;T:publicTI"lib/forwardable.rb;T[U:RDoc::Constant[i I"VERSION;TI"Forwardable::VERSION;T;0o;;[o; ;[I" Version of +forwardable.rb+;T;@};0@}@cRDoc::NormalModule0U;[i I"FORWARDABLE_VERSION;TI"%Forwardable::FORWARDABLE_VERSION;T;0o;;[ ;@};0@}@@�0[ [[I" class;T[[;[ [:protected[ [:private[ [I" instance;T[[;[[I"def_delegator;T@�[I"def_delegators;T@�[I"def_instance_delegator;T@�[I"def_instance_delegators;T@�[I" delegate;T@�[I"instance_delegate;T@�[;[ [;[ [ [U:RDoc::Context::Section[i 0o;;[ ;0;0[@}@{@{cRDoc::TopLevel