ruby-changes:62165
From: Masataka <ko1@a...>
Date: Fri, 10 Jul 2020 07:01:28 +0900 (JST)
Subject: [ruby-changes:62165] ba81bc24e6 (master): Add instance_methods to class generated by DelegateClass
https://git.ruby-lang.org/ruby.git/commit/?id=ba81bc24e6 From ba81bc24e62d03cc0f4c05cf4e2b378696631568 Mon Sep 17 00:00:00 2001 From: Masataka Pocke Kuwabara <kuwabara@p...> Date: Fri, 10 Jul 2020 07:01:10 +0900 Subject: Add instance_methods to class generated by DelegateClass Also, make DelegateClass.instance_method fallback to superclass. Fixes [Bug #16982] diff --git a/lib/delegate.rb b/lib/delegate.rb index d6fb90b..a1fded6 100644 --- a/lib/delegate.rb +++ b/lib/delegate.rb @@ -422,6 +422,21 @@ def DelegateClass(superclass, &block) https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L422 klass.define_singleton_method :protected_instance_methods do |all=true| super(all) | superclass.protected_instance_methods end + klass.define_singleton_method :instance_methods do |all=true| + super(all) | superclass.instance_methods + end + klass.define_singleton_method :public_instance_method do |name| + super(name) + rescue NameError + raise unless self.public_instance_methods.include?(name) + superclass.public_instance_method(name) + end + klass.define_singleton_method :instance_method do |name| + super(name) + rescue NameError + raise unless self.instance_methods.include?(name) + superclass.instance_method(name) + end klass.module_eval(&block) if block return klass end diff --git a/test/test_delegate.rb b/test/test_delegate.rb index 4a3445f..57480b1 100644 --- a/test/test_delegate.rb +++ b/test/test_delegate.rb @@ -106,6 +106,10 @@ class TestDelegateClass < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_delegate.rb#L106 protected def parent_protected; end + + private + + def parent_private; end end class Child < DelegateClass(Parent) @@ -117,6 +121,10 @@ class TestDelegateClass < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_delegate.rb#L121 protected def parent_protected_added; end + + private + + def parent_private_added; end end def test_public_instance_methods @@ -131,6 +139,32 @@ class TestDelegateClass < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_delegate.rb#L139 assert_equal([:parent_protected, :parent_protected_added], (Child.new(Parent.new).protected_methods - ignores).sort) end + def test_instance_methods + ignores = Object.instance_methods | Delegator.instance_methods + assert_equal([:parent_protected, :parent_protected_added, :parent_public, :parent_public_added], (Child.instance_methods - ignores).sort) + assert_equal([:parent_protected, :parent_protected_added, :parent_public, :parent_public_added], (Child.new(Parent.new).methods - ignores).sort) + end + + def test_DelegateClass_instance_method + assert_instance_of UnboundMethod, Child.instance_method(:parent_public) + assert_instance_of UnboundMethod, Child.instance_method(:parent_public_added) + assert_instance_of UnboundMethod, Child.instance_method(:parent_protected) + assert_instance_of UnboundMethod, Child.instance_method(:parent_protected_added) + assert_raise(NameError) { Child.instance_method(:parent_private) } + assert_raise(NameError) { Child.instance_method(:parent_private_added) } + assert_instance_of UnboundMethod, Child.instance_method(:to_s) + end + + def test_DelegateClass_public_instance_method + assert_instance_of UnboundMethod, Child.public_instance_method(:parent_public) + assert_instance_of UnboundMethod, Child.public_instance_method(:parent_public_added) + assert_raise(NameError) { Child.public_instance_method(:parent_protected) } + assert_raise(NameError) { Child.public_instance_method(:parent_protected_added) } + assert_raise(NameError) { Child.instance_method(:parent_private) } + assert_raise(NameError) { Child.instance_method(:parent_private_added) } + assert_instance_of UnboundMethod, Child.public_instance_method(:to_s) + end + class IV < DelegateClass(Integer) attr_accessor :var -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/