Skip to content

Commit

Permalink
Merge pull request #77 from smortex/eql
Browse files Browse the repository at this point in the history
Make eql? behare like ==.
  • Loading branch information
bluemonk authored Jan 8, 2022
2 parents 85b95ef + 9b8d8fd commit 8ff186b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/ipaddress/ipv4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ def <=>(oth)
return prefix <=> oth.prefix if to_u32 == oth.to_u32
to_u32 <=> oth.to_u32
end
alias eql? ==

#
# Returns the number of IP addresses included
Expand Down
1 change: 1 addition & 0 deletions lib/ipaddress/ipv6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ def <=>(oth)
return prefix <=> oth.prefix if to_u128 == oth.to_u128
to_u128 <=> oth.to_u128
end
alias eql? ==

#
# Returns the address portion of an IP in binary format,
Expand Down
2 changes: 2 additions & 0 deletions test/ipaddress/ipv4_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,10 @@ def test_method_compare
assert_equal false, ip3 < ip1
# ip1 should be equal to itself
assert_equal true, ip1 == ip1
assert_equal true, ip1.eql?(ip1)
# ip1 should be equal to ip4
assert_equal true, ip1 == ip4
assert_equal true, ip1.eql?(ip4)
# test sorting
arr = ["10.1.1.1/8","10.1.1.1/16","172.16.1.1/14"]
assert_equal arr, [ip1,ip2,ip3].sort.map{|s| s.to_string}
Expand Down
5 changes: 5 additions & 0 deletions test/ipaddress/ipv6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ def test_method_compare
assert_equal false, ip3 < ip1
# ip1 should be equal to itself
assert_equal true, ip1 == ip1
assert_equal true, ip1.eql?(ip1)
# ip1 should be equal to a copy of itself
other = ip1.dup
assert_equal true, ip1 == other
assert_equal true, ip1.eql?(other)
# ip4 should be greater than ip1
assert_equal true, ip1 < ip4
assert_equal false, ip1 > ip4
Expand Down

0 comments on commit 8ff186b

Please sign in to comment.