Issue #11531 has been updated by jeremyevans0 (Jeremy Evans).


I am not sure whether this is a bug. `eql?` considers the netmask, but `==` does not.  So if you want to consider the netmask, you can currently use `eql?`. Changing `==` to be the same as `eql?` could cause backwards compatibility issues.

The major problem is one of design, in that `IPAddr` can operate as either a specific IP address or as a network/CIDR-block.  I think it would have been better to use separate classes for those two concepts, but that is not fixable with the current design. 

----------------------------------------
Bug #11531: IPAddr#== implements wrong logic
https://bugs.ruby-lang.org/issues/11531#change-82400

* Author: panasyuk (Aleksander Panasyuk)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 2.1.5p273
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
# Description
IPAddr#== should implement the logic of comparison of two IPAddr instances. This generally means that it compares two IP addresses.
Lets look at the code of this method:

https://github.com/ruby/ruby/blob/c8b3f1b470e343e7408ab5883f046b1056d94ccc/lib/ipaddr.rb#L151

`return @family == other.family && @addr == other.to_i`

It returns the result of comparison of the families and the addresses, but it should also compare the netmask which describes the network where this address is located.
The code below shows the test case for this comparison:
`
ip1 = IPAddr.new '195.51.100.0/24'
ip2 = IPAddr.new '195.51.100.0/26'
ip1 == ip2 #=> true
`
This code shows that two identical IP addresses from different networks are equal. But the result should be `false` because these addresses are not identical.

# Possible solution
Depending on Feature #11210 i would propose following implementation of this method:
`
def ==(other)
  other = coerce_other(other)
  return @family == other.family && @addr == other.to_i && @mask_addr == other.netmask
end
`



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>