Skip to content

Commit ff45510

Browse files
committed
Hash#== performs numeric conversions, like built-in Ruby classes
1 parent c0adef5 commit ff45510

3 files changed

Lines changed: 8 additions & 2 deletions

File tree

lib/immutable/enumerable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def <=>(other)
107107
# Return true if `other` contains the same elements, in the same order.
108108
# @return [Boolean]
109109
def ==(other)
110-
self.eql?(other) || other.respond_to?(:to_ary) && to_ary.eql?(other.to_ary)
110+
self.eql?(other) || (other.respond_to?(:to_ary) && to_ary == other.to_ary)
111111
end
112112

113113
# Convert all the elements into strings and join them together, separated by

lib/immutable/hash.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ def eql?(other)
771771
# @param other [Object] The object to compare with
772772
# @return [Boolean]
773773
def ==(other)
774-
self.eql?(other) || (other.respond_to?(:to_hash) && to_hash.eql?(other.to_hash))
774+
self.eql?(other) || (other.respond_to?(:to_hash) && to_hash == other.to_hash)
775775
end
776776

777777
# Return true if this `Hash` is a proper superset of `other`, which means

spec/lib/immutable/hash/eql_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "spec_helper"
2+
require "bigdecimal"
23

34
describe Immutable::Hash do
45
let(:hash) { H["A" => "aye", "B" => "bee", "C" => "see"] }
@@ -33,6 +34,11 @@
3334
instance = subclass.new("A" => "aye", "B" => "bee", "C" => "see")
3435
(hash == instance).should == true
3536
end
37+
38+
it "performs numeric conversions between floats and BigDecimals" do
39+
expect(H[a: 0.0] == H[a: BigDecimal('0.0')]).to be true
40+
expect(H[a: BigDecimal('0.0')] == H[a: 0.0]).to be true
41+
end
3642
end
3743

3844
[:eql?, :==].each do |method|

0 commit comments

Comments
 (0)