ruby-changes:28787
From: zzak <ko1@a...>
Date: Mon, 20 May 2013 04:59:10 +0900 (JST)
Subject: [ruby-changes:28787] zzak:r40839 (trunk): * lib/forwardable.rb: Forwardable examples in overview were broken
zzak 2013-05-20 04:58:56 +0900 (Mon, 20 May 2013) New Revision: 40839 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40839 Log: * lib/forwardable.rb: Forwardable examples in overview were broken Based on patch by @joem [Fixes GH-303] [Bug #8392] Modified files: trunk/ChangeLog trunk/lib/forwardable.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 40838) +++ ChangeLog (revision 40839) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Mon May 20 04:56:59 2013 Zachary Scott <zachary@z...> + + * 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 <zachary@z...> * lib/optparse.rb: nodoc Object::Version and SPLAT_PROC Index: lib/forwardable.rb =================================================================== --- lib/forwardable.rb (revision 40838) +++ lib/forwardable.rb (revision 40839) @@ -19,30 +19,40 @@ https://github.com/ruby/ruby/blob/trunk/lib/forwardable.rb#L19 # #record_number(), which simply calls #[] on the <tt>@records</tt> # 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. -# -# printer = String.new -# printer.extend Forwardable # prepare object for delegation -# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts() -# printer.puts "Howdy!" +# +# 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] +# +# 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 # -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/