bug in IPAddr eql? (stndard library)

N

Nick Brown

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.
 
M

Martin Boese

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
 
N

Nick Brown

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ipaddr and invalid ipv4 addresses 3
Problem with IPAddr 7
No accept Ipv6 in IPAddr 2
IO.pos bug? 5
Standardlib "ipaddr.rb" 6
Strange bug in irb1.9 7
Incorrect -0.0.hash - Ruby bug. 5
:IRB Bug 1

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top