# IPAddr as Hash table key and accessing the mask\_addr

**URL:** <https://www.ruby-forum.com/t/ipaddr-as-hash-table-key-and-accessing-the-mask-addr/113320>\
**Category:** Ruby\
**Created:** [August 29, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/ipaddr-as-hash-table-key-and-accessing-the-mask-addr/113320 "2007-08-29T20:06:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ninj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9fc29f/32.png) [@ninj](https://www.ruby-forum.com/u/ninj)\
**Post date:** [August 29, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/ipaddr-as-hash-table-key-and-accessing-the-mask-addr/113320/1 "2007-08-29T20:06:34Z")

</div>

Hi,

I’m using IPAddr objects as hash keys. So I need the hash and eql?  
function as described in this post from the year 2004, which I’m  
reposting below

It looks like a very simple and useful addition to the class. Could  
someone please consider?

While we are on the subject of IPAddr… I also want to be able to  
retrieve the netmask of the IP Address like this:

netrange = IPAddr.new “192.168.1.0/24”  
netmask\_of\_netrange = netrange.mask\_addr # =\> 24

I’m using the class as a nice IP address/netrange parser as well.

So, in all, I’m proposing a patch

class IPAddr  
attr\_reader :mask\_addr  
def hash  
@addr  
end  
alias eql? ==  
end

I can already do all these locally, but I thought it’d be sensible to  
be included in the standard library. What do you think?

Nigel.

On Jan 12 2004, 4:54 pm, Neil S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
