Created
November 27, 2008 08:28
-
-
Save brainopia/29706 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# based on http://www.ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html | |
require 'delegate' | |
class SimpleDelegator < Delegator | |
def initialize(obj) | |
super # pass obj to Delegator constructor, required | |
@_sd_obj = obj # store obj for future use | |
end | |
def __getobj__ | |
@_sd_obj # return object we are delegating to, required | |
end | |
def __setobj__(obj) | |
@_sd_obj = obj # change delegation object, a feature we're providing | |
end | |
end | |
proxy_object = SimpleDelegator.new [] | |
proxy_object.push 'first', 'second' | |
p proxy_object.size # => 2 | |
p proxy_object # => ["first", "second"] | |
proxy_object.__setobj__ 'hey' | |
p proxy_object # => "hey" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment