[前][次][番号順一覧][スレッド一覧]

ruby-changes:29853

From: zzak <ko1@a...>
Date: Thu, 11 Jul 2013 00:50:34 +0900 (JST)
Subject: [ruby-changes:29853] zzak:r41905 (trunk): * lib/delegate.rb: Add example for __setobj__ and __getobj__

zzak	2013-07-11 00:50:23 +0900 (Thu, 11 Jul 2013)

  New Revision: 41905

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41905

  Log:
    * lib/delegate.rb: Add example for __setobj__ and __getobj__
      [Bug #8615] Patch by Caleb Thompson

  Modified files:
    trunk/ChangeLog
    trunk/lib/delegate.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41904)
+++ ChangeLog	(revision 41905)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Jul 11 00:48:29 2013  Zachary Scott  <zachary@z...>
+
+	* lib/delegate.rb: Add example for __setobj__ and __getobj__
+	  [Bug #8615] Patch by Caleb Thompson
+
 Wed Jul 10 23:29:22 2013  Zachary Scott  <zachary@z...>
 
 	* lib/logger.rb: Use :call-seq: for method signature rdoc
Index: lib/delegate.rb
===================================================================
--- lib/delegate.rb	(revision 41904)
+++ lib/delegate.rb	(revision 41905)
@@ -230,6 +230,34 @@ end https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L230
 # and even to change the object being delegated to at a later time with
 # #__setobj__.
 #
+#   class User
+#     def born_on
+#       Date.new(1989, 09, 10)
+#     end
+#   end
+#
+#   class UserDecorator < SimpleDelegator
+#     def birth_year
+#       born_on.year
+#     end
+#   end
+#
+#   decorated_user = UserDecorator.new(User.new)
+#   decorated_user.birth_year  #=> 1989
+#   decorated_user.__getobj__  #=> #<User: ...>
+#
+# A SimpleDelegator instance can take advantage of the fact that SimpleDelegator
+# is a subclass of +Delegator+ to call <tt>super</tt> to have methods called on
+# the object being delegated to.
+#
+#   class SuperArray < SimpleDelegator
+#     def [](*args)
+#       super + 1
+#     end
+#   end
+#
+#   SuperArray.new([1])[0]  #=> 2
+#
 # Here's a simple example that takes advantage of the fact that
 # SimpleDelegator's delegation object can be changed at any time.
 #

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]