Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/ipaddress/ipv4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,13 @@ def each
end
end

# Equality operator
def ==(oth)
oth.class == self.class && \
oth.to_u32 == self.to_u32 && \
oth.prefix == self.prefix
end

#
# Spaceship operator to compare IPv4 objects
#
Expand Down
7 changes: 7 additions & 0 deletions lib/ipaddress/ipv6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,13 @@ def each
end
end

# Equality operator
def ==(oth)
oth.class == self.class && \
oth.prefix == self.prefix && \
oth.to_u128 == self.to_u128
end

#
# Spaceship operator to compare IPv6 objects
#
Expand Down
4 changes: 4 additions & 0 deletions test/ipaddress/ipv4_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ def test_method_compare
assert_equal true, ip1 == ip1
# ip1 should be equal to ip4
assert_equal true, ip1 == ip4
# ip1 should be unequal to ip2
assert_equal false, ip1 == ip2
# ip1 should be unequal to nil
assert_equal false, ip1 == nil
# 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
4 changes: 4 additions & 0 deletions test/ipaddress/ipv6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ def test_method_compare
# ip4 should be greater than ip1
assert_equal true, ip1 < ip4
assert_equal false, ip1 > ip4
# ip1 should be unequal to ip2
assert_equal false, ip1 == ip2
# ip1 should be unequal to nil
assert_equal false, ip1 == nil
# test sorting
arr = ["2001:db8:1::1/64","2001:db8:1::1/65",
"2001:db8:1::2/64","2001:db8:2::1/64"]
Expand Down