From: Todd Benson Date: 2008-01-16T20:01:58+09:00 Subject: Re: Problem with IPAddr On Jan 15, 2008 11:53 PM, Bryan Richardson wrote: > Gary, > > Thanks for the reply. Here's what I get when I try something else: > > irb(main):001:0> require 'ipaddr' > => true > irb(main):002:0> net_1 = IPAddr.new("192.168.0.0/16") > => # > irb(main):003:0> net_2 = IPAddr.new("192.168.101.0/24") > => # > irb(main):004:0> net_1.include?(net_2) > => true > irb(main):005:0> net_2.include?(net_1) > => false > irb(main):006:0> > > Makes sense I guess. Is there any way to tell IPAddr to look at it as a > network instead of as an address? I have a situation where I may come > across an address (ending in .0, so defining a network) where the netmask > isn't given, so I assign it as a small network (say /29). Later on I may > come across it again, this time with the netmask defined (say /16), and I > want to merge the first one into the second one. Therefore, each time I add > a new network to my database, I compare it to the existing ones to see if > any should be merged. Any suggestions? Thanks!! -- BTR You could open up IPAddr to get at the mask for comparison. class IPAddr def net @mask_addr end end ip1 = IPAddr.new("192.168.0.0/16") ip2 = IPAddr.new("192.168.0.0/24") ip1.net > ip2.net # => false (which seems contradictory, but the mask number is larger for smaller networks) ip2.net > ip1.net #=> true hth, Todd