Skip to content

Commit 3cccf89

Browse files
committed
♻️ Match subclasses in SearchResult#eql?/#hash
Copying the behavior of `Array#eql?` and `Array#hash`, this updates both `SearchResult#eql?` and `SearchResult#hash` to match explicitly on `SearchResult` rather than the specific `self.class`. This way, they also match subclass instances (assuming the subclass doesn't override these methods). `#hash` was also re-ordered to match `#eql?`. This isn't needed for performance. It just makes it a little bit easier to identify that the two methods are comparing the same properties.
1 parent 803efb1 commit 3cccf89

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

lib/net/imap/search_result.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ def ==(other)
7373
end
7474

7575
# Hash equality. Unlike #==, order will be taken into account.
76-
def hash = [super, self.class, modseq].hash
76+
def hash = [SearchResult, modseq, super].hash
7777

7878
# Hash equality. Unlike #==, order will be taken into account.
7979
def eql?(other)
80-
self.class == other.class &&
80+
SearchResult === other &&
8181
modseq == other.modseq &&
8282
super
8383
end

test/net/imap/test_search_result.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ class SearchDataTests < Net::IMAP::TestCase
9393
refute_operator result.hash, :eql?, array.hash
9494
end
9595

96+
# NOTE: this subclass is NOT overriding #==, #hash, or #eql?
97+
Subclass = Class.new(SearchResult)
98+
99+
test "SearchResult[...] == / eql? Subclass[...]" do
100+
array = [1, 5, 20, 3, 98]
101+
result = SearchResult[*array]
102+
subclass = Subclass[*array]
103+
assert_operator result, :eql?, subclass
104+
assert_equal result.hash, subclass.hash
105+
modseq = 12345
106+
result = SearchResult[*array, modseq:]
107+
subclass = Subclass[*array, modseq:]
108+
assert_operator result, :eql?, subclass
109+
assert_equal result.hash, subclass.hash
110+
end
111+
96112
test "SearchResult[*nz_numbers, modseq: nz_number] != / not eql? Array[*nz_numbers]" do
97113
array = [1, 5, 20, 3, 98]
98114
result = SearchResult[*array, modseq: 123456]

0 commit comments

Comments
 (0)