1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/delegate] Fix DelegateClass block "method redefined" warning

This commit prevents "method redefined" warnings when overriding methods
within a `DelegateClass` block, such as in the following example:

  ```ruby
  Base = Class.new do
    def foo
      "foo"
    end
  end

  Overridden = DelegateClass(Base) do
    def foo
      super + "!"
    end
  end
  ```

Fixes https://bugs.ruby-lang.org/issues/19047.

https://github.com/ruby/delegate/commit/214fae86de
This commit is contained in:
Jonathan Hefner 2022-10-11 12:02:15 -05:00 committed by git
parent d416205341
commit 6061003100
2 changed files with 14 additions and 0 deletions

View file

@ -412,10 +412,12 @@ def DelegateClass(superclass, &block)
end end
protected_instance_methods.each do |method| protected_instance_methods.each do |method|
define_method(method, Delegator.delegating_block(method)) define_method(method, Delegator.delegating_block(method))
alias_method(method, method)
protected method protected method
end end
public_instance_methods.each do |method| public_instance_methods.each do |method|
define_method(method, Delegator.delegating_block(method)) define_method(method, Delegator.delegating_block(method))
alias_method(method, method)
end end
end end
klass.define_singleton_method :public_instance_methods do |all=true| klass.define_singleton_method :public_instance_methods do |all=true|

View file

@ -29,6 +29,18 @@ class TestDelegateClass < Test::Unit::TestCase
assert_equal(1, klass.new([1]).foo) assert_equal(1, klass.new([1]).foo)
end end
def test_delegate_class_block_with_override
warning = EnvUtil.verbose_warning do
klass = DelegateClass(Array) do
def first
super.inspect
end
end
assert_equal("1", klass.new([1]).first)
end
assert_empty(warning)
end
def test_systemcallerror_eq def test_systemcallerror_eq
e = SystemCallError.new(0) e = SystemCallError.new(0)
assert((SimpleDelegator.new(e) == e) == (e == SimpleDelegator.new(e)), "[ruby-dev:34808]") assert((SimpleDelegator.new(e) == e) == (e == SimpleDelegator.new(e)), "[ruby-dev:34808]")