ruby-changes:47052
From: nobu <ko1@a...>
Date: Sat, 24 Jun 2017 12:35:35 +0900 (JST)
Subject: [ruby-changes:47052] nobu:r59167 (trunk): Delegate to `eql?` [Fix GH-1564]
nobu 2017-06-24 12:35:29 +0900 (Sat, 24 Jun 2017) New Revision: 59167 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59167 Log: 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@g...>. [ruby-core:76950] [Bug #12684] Modified files: trunk/lib/delegate.rb trunk/spec/rubyspec/library/delegate/delegator/eql_spec.rb trunk/test/test_delegate.rb Index: spec/rubyspec/library/delegate/delegator/eql_spec.rb =================================================================== --- spec/rubyspec/library/delegate/delegator/eql_spec.rb (revision 59166) +++ spec/rubyspec/library/delegate/delegator/eql_spec.rb (revision 59167) @@ -2,10 +2,34 @@ require File.expand_path('../../../../sp https://github.com/ruby/ruby/blob/trunk/spec/rubyspec/library/delegate/delegator/eql_spec.rb#L2 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 Index: lib/delegate.rb =================================================================== --- lib/delegate.rb (revision 59166) +++ lib/delegate.rb (revision 59167) @@ -40,7 +40,7 @@ class Delegator < BasicObject https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L40 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| @@ -146,6 +146,14 @@ class Delegator < BasicObject https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L146 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\_\_ # def ! Index: test/test_delegate.rb =================================================================== --- test/test_delegate.rb (revision 59166) +++ test/test_delegate.rb (revision 59167) @@ -3,6 +3,14 @@ require 'test/unit' https://github.com/ruby/ruby/blob/trunk/test/test_delegate.rb#L3 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::Te https://github.com/ruby/ruby/blob/trunk/test/test_delegate.rb#L127 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/