mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/delegate.rb (Delegator): now inherits BasicObject.
[ruby-dev:39154], [Bug #2679], [ruby-dev:40242] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26566 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
7a4621eecf
commit
10e8419da6
3 changed files with 38 additions and 16 deletions
|
@ -1,3 +1,8 @@
|
|||
Thu Feb 4 08:15:53 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* lib/delegate.rb (Delegator): now inherits BasicObject.
|
||||
[ruby-dev:39154], [Bug #2679], [ruby-dev:40242]
|
||||
|
||||
Thu Feb 4 03:00:59 2010 Yusuke Endoh <mame@tsg.ne.jp>
|
||||
|
||||
* ext/bigdecimal/bigdecimal.c (BigDecimal_DoDivmod): get red of
|
||||
|
|
|
@ -114,11 +114,21 @@
|
|||
# subclasses. Subclasses should redefine \_\_getobj\_\_. For a concrete
|
||||
# implementation, see SimpleDelegator.
|
||||
#
|
||||
class Delegator
|
||||
[:to_s,:inspect,:=~,:!~,:===,:<=>].each do |m|
|
||||
undef_method m
|
||||
class Delegator < BasicObject
|
||||
# :stopdoc:
|
||||
def class
|
||||
(class << self; self; end).superclass
|
||||
end
|
||||
|
||||
def extend(*mods)
|
||||
(class << self; self; end).class_eval {include(*mods)}
|
||||
end
|
||||
|
||||
def self.const_missing(n)
|
||||
::Object.const_get(n)
|
||||
end
|
||||
# :startdoc:
|
||||
|
||||
#
|
||||
# Pass in the _obj_ to delegate method calls to. All methods supported by
|
||||
# _obj_ will be delegated to.
|
||||
|
@ -131,12 +141,12 @@ class Delegator
|
|||
def method_missing(m, *args, &block)
|
||||
begin
|
||||
target = self.__getobj__
|
||||
unless target.respond_to?(m)
|
||||
unless target.respond_to?(m, true)
|
||||
super(m, *args, &block)
|
||||
else
|
||||
target.__send__(m, *args, &block)
|
||||
end
|
||||
rescue Exception
|
||||
rescue ::Exception
|
||||
if i = $@.index{|s| %r"\A#{Regexp.quote(__FILE__)}:\d+:in `method_missing'\z"o =~ s}
|
||||
$@[0..i] = []
|
||||
end
|
||||
|
@ -248,17 +258,6 @@ class SimpleDelegator<Delegator
|
|||
raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
|
||||
@delegate_sd_obj = obj
|
||||
end
|
||||
|
||||
def initialize(obj) # :nodoc:
|
||||
(self.public_methods - Delegator.public_api).each do |m|
|
||||
class << self
|
||||
self
|
||||
end.class_eval do
|
||||
undef_method m
|
||||
end
|
||||
end
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
# :stopdoc:
|
||||
|
|
|
@ -34,12 +34,20 @@ class TestDelegateClass < Test::Unit::TestCase
|
|||
def m
|
||||
:o
|
||||
end
|
||||
private
|
||||
def delegate_test_m
|
||||
:o
|
||||
end
|
||||
end
|
||||
|
||||
class Foo
|
||||
def m
|
||||
:m
|
||||
end
|
||||
private
|
||||
def delegate_test_m
|
||||
:m
|
||||
end
|
||||
end
|
||||
|
||||
class Bar < DelegateClass(Foo)
|
||||
|
@ -50,6 +58,9 @@ class TestDelegateClass < Test::Unit::TestCase
|
|||
assert_equal(:m, Foo.new.m)
|
||||
assert_equal(:m, SimpleDelegator.new(Foo.new).m)
|
||||
assert_equal(:m, Bar.new(Foo.new).m)
|
||||
bug = '[ruby-dev:39154]'
|
||||
assert_equal(:m, SimpleDelegator.new(Foo.new).__send__(:delegate_test_m), bug)
|
||||
assert_equal(:m, Bar.new(Foo.new).__send__(:delegate_test_m), bug)
|
||||
end
|
||||
|
||||
class IV < DelegateClass(Integer)
|
||||
|
@ -67,4 +78,11 @@ class TestDelegateClass < Test::Unit::TestCase
|
|||
d = Marshal.load(Marshal.dump(c))
|
||||
assert_equal(1, d.var, bug1744)
|
||||
end
|
||||
|
||||
def test_copy_frozen
|
||||
bug2679 = '[ruby-dev:40242]'
|
||||
a = [42, :hello].freeze
|
||||
d = SimpleDelegator.new(a)
|
||||
assert_nothing_raised(bug2679) {d.dup[0] += 1}
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue