From de3bff164c3b8405f40d2e89cf726f7e865102d1 Mon Sep 17 00:00:00 2001 From: matz Date: Thu, 29 Dec 2005 12:05:16 +0000 Subject: * eval.c (rb_mod_define_method): should save safe_level in the proc object. [ruby-dev:28146] * test/drb/drbtest.rb (DRbService::self.ext_service): increase timeout limit. a patch from Kazuhiro NISHIYAMA . [ruby-dev:28132] * eval.c (ev_const_get): fixed a bug in constant reference during instance_eval. [yarv-dev:707] * eval.c (ev_const_defined): ditto. * lib/yaml.rb (YAML::add_domain_type): typo fixed. a patch from Joel VanderWerf . [ruby-talk:165285] [ruby-core:6995] * ext/digest/sha2/sha2.c (ULL): support AIX C. a patch from Kailden . [ruby-core:06984] * ext/syck/rubyext.c (rb_syck_compile): avoid potential memory leak. * ext/syck/rubyext.c (syck_set_ivars): avoid potential memory leak by explicit symbol allocation. * lib/delegate.rb (Delegator::method_missing): should delegate block as well. * lib/cgi.rb (CGI::QueryExtension::MorphingBody): fix criteria to use Tempfile. A fix from Zev Blut . [ruby-core:06076] * string.c: remove global functions work on $_. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9757 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/delegate.rb | 171 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 89 insertions(+), 82 deletions(-) (limited to 'lib/delegate.rb') diff --git a/lib/delegate.rb b/lib/delegate.rb index b54b57d7b7..3be335580b 100644 --- a/lib/delegate.rb +++ b/lib/delegate.rb @@ -121,61 +121,93 @@ class Delegator undef_method m end - # - # Pass in the _obj_ to delegate method calls to. All methods supported by - # _obj_ will be delegated to. - # - def initialize(obj) - __setobj__(obj) - end + module MethodDelegation + # + # Pass in the _obj_ to delegate method calls to. All methods supported by + # _obj_ will be delegated to. + # + def initialize(obj) + __setobj__(obj) + end - # Handles the magic of delegation through \_\_getobj\_\_. - def method_missing(m, *args) - begin - target = self.__getobj__ - unless target.respond_to?(m) - super(m, *args) + # Handles the magic of delegation through \_\_getobj\_\_. + 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| /^#{__FILE__}:\d+:in `method_missing'$/ =~ s} #` + ::Kernel::raise end - target.__send__(m, *args) - rescue Exception - $@.delete_if{|s| /^#{__FILE__}:\d+:in `method_missing'$/ =~ s} #` - ::Kernel::raise end - end - # - # Checks for a method provided by this the delegate object by fowarding the - # call through \_\_getobj\_\_. - # - def respond_to?(m) - return true if super - return self.__getobj__.respond_to?(m) - end + # + # Checks for a method provided by this the delegate object by fowarding the + # call through \_\_getobj\_\_. + # + def respond_to?(m) + return true if super + return self.__getobj__.respond_to?(m) + end - # - # This method must be overridden by subclasses and should return the object - # method calls are being delegated to. - # - def __getobj__ - raise NotImplementedError, "need to define `__getobj__'" - end + # + # Returns true if two objects are considered same. + # + def ==(obj) + return true if obj.equal?(self) + self.__getobj__ == obj + end - # - # This method must be overridden by subclasses and change the object delegate - # to _obj_. - # - def __setobj__(obj) - raise NotImplementedError, "need to define `__setobj__'" - end + # + # Returns true only if two objects are identical. + # + def equal?(obj) + self.object_id == obj.object_id + end - # Serialization support for the object returned by \_\_getobj\_\_. - def marshal_dump - __getobj__ - end - # Reinitializes delegation from a serialized object. - def marshal_load(obj) - __setobj__(obj) + # + # This method must be overridden by subclasses and should return the object + # method calls are being delegated to. + # + def __getobj__ + raise NotImplementedError, "need to define `__getobj__'" + end + + # + # This method must be overridden by subclasses and change the object delegate + # to _obj_. + # + def __setobj__(obj) + raise NotImplementedError, "need to define `__setobj__'" + end + + # Serialization support for the object returned by \_\_getobj\_\_. + def marshal_dump + __getobj__ + end + # Reinitializes delegation from a serialized object. + def marshal_load(obj) + __setobj__(obj) + end + + # Clone support for the object returned by \_\_getobj\_\_. + def clone + new = super + new.__setobj__(__getobj__.clone) + new + end + # Duplication support for the object returned by \_\_getobj\_\_. + def dup + new = super + new.__setobj__(__getobj__.dup) + new + end end + include MethodDelegation end # @@ -208,19 +240,6 @@ class SimpleDelegator true + p foo2.iter{[55,true]} # => true foo2.error # raise error! end -- cgit v1.2.3