Skip to content

Instantly share code, notes, and snippets.

@brainopia
Created November 27, 2008 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainopia/29706 to your computer and use it in GitHub Desktop.
Save brainopia/29706 to your computer and use it in GitHub Desktop.
# 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