Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comparison by using internal fields - address and prefix #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions lib/ipaddress/ipv4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,23 @@ def <=>(oth)
return prefix <=> oth.prefix if to_u32 == oth.to_u32
to_u32 <=> oth.to_u32
end

#
# Check if 2 IPs (ranges) are equal
#
# Example:
#
# ip1 = IPAddress '10.0.0.0/24'
# ip2 = IPAddress '10.0.0.0/24'
# ip3 = IPAddress '10.0.0.0/25'
#
# ip1.eql? ip2 # => true
# ip2.eql? ip1 # => true
# ip1.eql? ip3 # => false
def eql?(other)
(!other.is_a? self.class) || (prefix == other.prefix && to_u32 == other.to_u32)
end

alias eql? ==

#
Expand Down
20 changes: 19 additions & 1 deletion lib/ipaddress/ipv6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,27 @@ def each
#
def <=>(oth)
return nil unless oth.is_a?(self.class)
return prefix <=> oth.prefix if to_u128 == oth.to_u128
return prefix <=> oth.prefix if to_u128 == oth.to_u128
to_u128 <=> oth.to_u128
end

# Check if 2 IPs (ranges) are equal
#
# Example:
#
# ip1 = IPAddress "2001:db8:1::1/64"
# ip2 = IPAddress "2001:db8:1::1/64"
# ip3 = IPAddress "2001:db8:1::1/65"
# ip4 = IPAddress "2001:db8:2::1/65"
#
# ip1.eql? ip2 # => true
# ip2.eql? ip1 # => true
# ip1.eql? ip3 # => false
# ip4.eql? ip3 # => false
def eql?(other)
(!other.is_a? self.class) || (prefix == other.prefix && to_u128 == oth.to_u128)
end

alias eql? ==

#
Expand Down