Delegator is an abstract class used to build delegator pattern objects from subclasses. Subclasses should redefine __getobj__. For a concrete implementation, see SimpleDelegator.
Returns true if two objects are considered same.
# File delegate.rb, line 157
def ==(obj)
return true if obj.equal?(self)
self.__getobj__ == obj
end
This method must be overridden by subclasses and should return the object method calls are being delegated to.
# File delegate.rb, line 166
def __getobj__
raise NotImplementedError, "need to define `__getobj__'"
end
This method must be overridden by subclasses and change the object delegate to obj.
# File delegate.rb, line 174
def __setobj__(obj)
raise NotImplementedError, "need to define `__setobj__'"
end
Clone support for the object returned by __getobj__.
# File delegate.rb, line 188
def clone
new = super
new.__setobj__(__getobj__.clone)
new
end
Duplication support for the object returned by __getobj__.
# File delegate.rb, line 194
def dup
new = super
new.__setobj__(__getobj__.dup)
new
end
Serialization support for the object returned by __getobj__.
# File delegate.rb, line 179
def marshal_dump
__getobj__
end
Reinitializes delegation from a serialized object.
# File delegate.rb, line 183
def marshal_load(obj)
__setobj__(obj)
end
Handles the magic of delegation through __getobj__.
# File delegate.rb, line 131
def method_missing(m, *args, &block)
begin
target = self.__getobj__
unless target.respond_to?(m)
super(m, *args, &block)
else
target.__send__(m, *args, &block)
end
rescue Exception
$@.delete_if{|s| %r\A#{Regexp.quote(__FILE__)}:\d+:in `method_missing'\z" =~ s}
::Kernel::raise
end
end
Checks for a method provided by this the delegate object by fowarding the call through __getobj__.
# File delegate.rb, line 149
def respond_to?(m, include_private = false)
return true if super
return self.__getobj__.respond_to?(m, include_private)
end
Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.
If you want to help improve the Ruby documentation, please see Improve the docs, or visit Documenting-ruby.org.