Delegate to `eql?` [Fix GH-1564]

* lib/delegate.rb (eql?): Delegate to `eql?` of the inner object.
  based on the patch by giginet <giginet.net@gmail.com>.
  [ruby-core:76950] [Bug #12684]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59167 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-06-24 03:35:29 +00:00
Родитель dc62793a91
Коммит 3c45a7899e
3 изменённых файлов: 56 добавлений и 4 удалений

Просмотреть файл

@ -40,7 +40,7 @@ class Delegator < BasicObject
kernel = ::Kernel.dup
kernel.class_eval do
alias __raise__ raise
[:to_s, :inspect, :=~, :!~, :===, :<=>, :eql?, :hash].each do |m|
[:to_s, :inspect, :=~, :!~, :===, :<=>, :hash].each do |m|
undef_method m
end
private_instance_methods.each do |m|
@ -145,6 +145,14 @@ class Delegator < BasicObject
__getobj__ != obj
end
#
# Returns true if two objects are considered of equal value.
#
def eql?(obj)
return true if obj.equal?(self)
obj.eql?(__getobj__)
end
#
# Delegates ! to the \_\_getobj\_\_
#

Просмотреть файл

@ -2,10 +2,34 @@ require File.expand_path('../../../../spec_helper', __FILE__)
require File.expand_path('../../fixtures/classes', __FILE__)
describe "Delegator#eql?" do
it "is delegated" do
it "returns true when compared with same delegator" do
base = mock('base')
delegator = DelegateSpecs::Delegator.new(base)
base.should_receive(:eql?).with(42).and_return(:foo)
delegator.eql?(42).should == :foo
delegator.eql?(delegator).should be_true
end
it "returns true when compared with the inner object" do
base = mock('base')
delegator = DelegateSpecs::Delegator.new(base)
delegator.eql?(base).should be_true
end
it "returns false when compared with the delegator with other object" do
base = mock('base')
other = mock('other')
delegator0 = DelegateSpecs::Delegator.new(base)
delegator1 = DelegateSpecs::Delegator.new(other)
delegator0.eql?(delegator1).should be_false
end
it "returns false when compared with the other object" do
base = mock('base')
other = mock('other')
delegator = DelegateSpecs::Delegator.new(base)
delegator.eql?(other).should be_false
end
end

Просмотреть файл

@ -3,6 +3,14 @@ require 'test/unit'
require 'delegate'
class TestDelegateClass < Test::Unit::TestCase
module PP
def mu_pp(obj)
str = super
str = "#<#{obj.class}: #{str}>" if Delegator === obj
str
end
end
module M
attr_reader :m
end
@ -119,6 +127,18 @@ class TestDelegateClass < Test::Unit::TestCase
assert_equal([:bar], s.methods(false))
end
def test_eql?
extend PP
s0 = SimpleDelegator.new("foo")
s1 = SimpleDelegator.new("bar")
s2 = SimpleDelegator.new("foo")
assert_operator(s0, :eql?, s0)
assert_operator(s0, :eql?, "foo")
assert_operator(s0, :eql?, s2)
assert_not_operator(s0, :eql?, s1)
assert_not_operator(s0, :eql?, "bar")
end
class Foo
private
def delegate_test_private