diff --git a/ChangeLog b/ChangeLog index c7e270be8f..f1635fd713 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,12 @@ Thu Aug 11 23:29:03 2005 Nobuyoshi Nakada * ext/stringio/stringio.c: keep holding string after closed. +Thu Aug 11 20:48:40 2005 Tadashi Saito + + * numeric.c (fix_equal, fix_cmp, fix_gt, fix_ge, fix_lt, fix_le): + reduce coercing when a method knows about a operand type. + [ruby-dev:26789] + Thu Aug 11 13:01:48 2005 Kouhei Sutou * lib/rss: fixed sort bug. [ruby-list:41018] @@ -26,6 +32,11 @@ Thu Aug 11 13:01:48 2005 Kouhei Sutou (RSS::TestSetupMaker10::test_setup_maker_items_sort): added some tests for RSS::Maker::ItemsBase#do_sort. +Wed Aug 10 12:01:20 2005 Yukihiro Matsumoto + + * lib/delegate.rb: simplifies Delegator classes; SimpleDelegator + now uses method_missing for all methods. + Wed Aug 10 10:38:50 2005 Yukihiro Matsumoto * bignum.c (rb_big_mul0): multiply two numbers (x, y) without @@ -251,12 +262,12 @@ Wed Aug 3 21:59:16 2005 Hidetoshi NAGAI Wed Aug 3 12:40:28 2005 Tadashi Saito - * numeric.c (fix_minus, fix_mul, fix_quo, fix_div, fix_mod, - fix_divmod, fix_pow): ditto. - * numeric.c (fix_plus): reduce coercing when a method knows about a operand type. [ruby-dev:26723] + * numeric.c (fix_minus, fix_mul, fix_quo, fix_div, fix_mod, + fix_divmod, fix_pow): ditto. + * bignum.c (rb_big_div, rb_big_modulo): export to reduce coercing. diff --git a/lib/delegate.rb b/lib/delegate.rb index 1d0ebed07a..83aaf292a5 100644 --- a/lib/delegate.rb +++ b/lib/delegate.rb @@ -17,44 +17,27 @@ # end class Delegator + preserved = ["__id__", "object_id", "__send__", "respond_to?"] + instance_methods.each do |m| + next if preserved.include?(m) + undef_method m + end def initialize(obj) - preserved = ::Kernel.public_instance_methods(false) - preserved -= ["to_s","to_a","inspect","==","=~","==="] - for t in self.class.ancestors - preserved |= t.public_instance_methods(false) - preserved |= t.private_instance_methods(false) - preserved |= t.protected_instance_methods(false) - break if t == Delegator - end - preserved << "singleton_method_added" - for method in obj.methods - next if preserved.include? method - begin - eval <<-EOS - def self.#{method}(*args, &block) - begin - __getobj__.__send__(:#{method}, *args, &block) - rescue Exception - $@.delete_if{|s| /:in `__getobj__'$/ =~ s} #` - $@.delete_if{|s| /^\\(eval\\):/ =~ s} - ::Kernel::raise - end - end - EOS - rescue SyntaxError - raise NameError, "invalid identifier %s" % method, caller(4) - end - end + __setobj__(obj) end - alias initialize_methods initialize def method_missing(m, *args) - target = self.__getobj__ - unless target.respond_to?(m) - super(m, *args) + begin + target = self.__getobj__ + unless target.respond_to?(m) + super(m, *args) + end + target.__send__(m, *args) + rescue Exception + $@.delete_if{|s| /^#{__FILE__}:\d+:in `method_missing'$/ =~ s} #` + ::Kernel::raise end - target.__send__(m, *args) end def respond_to?(m) @@ -66,21 +49,19 @@ class Delegator raise NotImplementedError, "need to define `__getobj__'" end + def __setobj__(obj) + raise NotImplementedError, "need to define `__setobj__'" + end + def marshal_dump __getobj__ end def marshal_load(obj) - initialize_methods(obj) + __setobj__(obj) end end class SimpleDelegator [ref,...] @@id_rev_map = {} # ref -> obj - @@final = lambda{|id| + @@final = lambda {|id| + printf "final: %p\n", id __old_status = Thread.critical Thread.critical = true begin @@ -42,6 +43,7 @@ class WeakRef