Project

General

Profile

Bug #8615 ยป 0001-Add-examples-for-SimpleDelegator.patch

calebthompson (Caleb Thompson), 07/10/2013 03:41 AM

View differences:

lib/delegate.rb
230 230
# and even to change the object being delegated to at a later time with
231 231
# #__setobj__.
232 232
#
233
#   class User
234
#     def born_on
235
#       Date.new(1989, 09, 10)
236
#     end
237
#   end
238
#
239
#   class UserDecorator < SimpleDelegator
240
#     def birth_year
241
#       born_on.year
242
#     end
243
#   end
244
#
245
#   decorated_user = UserDecorator.new(User.new)
246
#   decorated_user.birth_year  #=> 1989
247
#   decorated_user.__getobj__  #=> #<User: ...>
248
#
249
# A SimpleDelegator instance can take advantage of the fact that SimpleDelegator
250
# is a subclass of +Delegator+ to call <tt>super</tt> to have methods called on
251
# the object being delegated to.
252
#
253
#   class SuperArray < SimpleDelegator
254
#     def [](*args)
255
#       super + 1
256
#     end
257
#   end
258
#
259
#   SuperArray.new([1])[0]  #=> 2
260
#
233 261
# Here's a simple example that takes advantage of the fact that
234 262
# SimpleDelegator's delegation object can be changed at any time.
235 263
#
236
-