From dbefdb434d43f99dfc4b8b558591ef63377ea43e Mon Sep 17 00:00:00 2001 From: zzak Date: Sun, 19 May 2013 19:58:56 +0000 Subject: [PATCH] * lib/forwardable.rb: Forwardable examples in overview were broken Based on patch by @joem [Fixes GH-303] [Bug #8392] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40839 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ lib/forwardable.rb | 34 ++++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index ba8d8f294b..cc939918d9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon May 20 04:56:59 2013 Zachary Scott + + * lib/forwardable.rb: Forwardable examples in overview were broken + Based on patch by @joem [Fixes GH-303] [Bug #8392] + Mon May 20 03:35:26 2013 Zachary Scott * lib/optparse.rb: nodoc Object::Version and SPLAT_PROC diff --git a/lib/forwardable.rb b/lib/forwardable.rb index 0f8807aac1..ecc5f03843 100644 --- a/lib/forwardable.rb +++ b/lib/forwardable.rb @@ -19,30 +19,40 @@ # #record_number(), which simply calls #[] on the @records # array, like this: # +# require 'forwardable' +# # class RecordCollection +# attr_accessor :records # extend Forwardable # def_delegator :@records, :[], :record_number # end # +# We can use the lookup method like so: +# +# r = RecordCollection.new +# r.records = [4,5,6] +# r.record_number(0) # => 4 +# # 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 +# class RecordCollection # re-open RecordCollection class # def_delegators :@records, :size, :<<, :map # end -# f = Foo.new -# f.printf ... -# f.gets -# f.content_at(1) # -# If the object isn't a Module and Class, You can too extend Forwardable -# module. +# r = RecordCollection.new +# r.records = [1,2,3] +# r.record_number(0) # => 1 +# r.size # => 3 +# r << 4 # => [1, 2, 3, 4] +# r.map { |x| x * 2 } # => [2, 4, 6, 8] # -# printer = String.new -# printer.extend Forwardable # prepare object for delegation -# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts() -# printer.puts "Howdy!" +# You can even extend regular objects with Forwardable. +# +# my_hash = Hash.new +# my_hash.extend Forwardable # prepare object for delegation +# my_hash.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts() +# my_hash.puts "Howdy!" # # == Another example #