# Bug in IPAddr eql? (stndard library)

**URL:** <https://www.ruby-forum.com/t/bug-in-ipaddr-eql-stndard-library/172772>\
**Category:** Ruby\
**Created:** [July 27, 2009, 8:50pm UTC](https://www.ruby-forum.com/t/bug-in-ipaddr-eql-stndard-library/172772 "2009-07-27T20:50:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [July 27, 2009, 8:50pm UTC](https://www.ruby-forum.com/t/bug-in-ipaddr-eql-stndard-library/172772/1 "2009-07-27T20:50:11Z")

</div>

The .eql? method is supposed to return true if the objects are == and of  
the same class, correct? This is _not_ the case with IPAddr objects.

irb(main):001:0\> require ‘ipaddr’  
=\> true  
irb(main):002:0\> a = IPAddr.new(‘1.1.1.1’)  
=\> #\<IPAddr: IPv4:1.1.1.1/255.255.255.255\>  
irb(main):003:0\> b = IPAddr.new(‘1.1.1.1’)  
=\> #\<IPAddr: IPv4:1.1.1.1/255.255.255.255\>  
irb(main):004:0\> a == b  
=\> true  
irb(main):005:0\> a.eql? b  
=\> false  
irb(main):006:0\> a == b and a.class == b.class  
=\> true

Clearly, the value of “a.eql? b” should be true, not false.

This bug breaks several things. In my case, it causes Sets of IPAddr  
objects to contain duplicates, angering the gods of mathematics.

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [July 28, 2009, 11:32am UTC](https://www.ruby-forum.com/t/bug-in-ipaddr-eql-stndard-library/172772/2 "2009-07-28T11:32:07Z")

</div>

Hi,

.eql? is implemented in Object - the parent of IPAddr. According to the  
docs ‘==’ is synonymous with ‘eql?’ in Object. Now IPAddr has it’s own  
implementation of ‘==’ but doesn’t touch the ‘eql?’ method.

This should quick fix your problem:

class IPAddr  
def eql?(other)  
self == other  
end  
end

I would also consider it as a bug.

See: ri Object.eql

Martin

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [July 28, 2009, 3:36pm UTC](https://www.ruby-forum.com/t/bug-in-ipaddr-eql-stndard-library/172772/3 "2009-07-28T15:36:01Z")

</div>

> I would also consider it as a bug.
> 
> See: ri Object.eql
> 
> Martin

Thanks, Martin. I have been looking into this more. It seems that for  
Sets, Hashes, and Arrays to work properly with an object, both .eql? and  
.hash must be defined in that object. I wrote a patch that implements  
both methods and sent it to Akinori Musha, the maintainer of IPAddr. He  
will be including my patch or something similar the next version.
