lib/ipaddress/ipv4.rb in ipaddress-0.7.0 vs lib/ipaddress/ipv4.rb in ipaddress-0.7.5

- old
+ new

@@ -380,23 +380,23 @@ # Iterates over all the hosts IP addresses for the given # network (or IP address). # # ip = IPAddress("10.0.0.1/29") # - # ip.each do |i| + # ip.each_host do |i| # p i.to_s # end # #=> "10.0.0.1" # #=> "10.0.0.2" # #=> "10.0.0.3" # #=> "10.0.0.4" # #=> "10.0.0.5" # #=> "10.0.0.6" # def each_host - hosts.each do |i| - yield i + (network_u32+1..broadcast_u32-1).each do |i| + yield self.class.parse_u32(i, @prefix) end end # # Iterates over all the IP addresses for the given @@ -518,12 +518,11 @@ end # # Checks whether a subnet includes the given IP address. # - # Accepts either string with the IP or and IPAddress::IPv4 - # object. + # Accepts an IPAddress::IPv4 object. # # ip = IPAddress("192.168.10.100/24") # # addr = IPAddress("192.168.10.102/24") # @@ -536,10 +535,50 @@ def include?(oth) @prefix <= oth.prefix and network_u32 == (oth.to_u32 & @prefix.to_u32) end # + # Checks whether a subnet includes all the + # given IPv4 objects. + # + # ip = IPAddress("192.168.10.100/24") + # + # addr1 = IPAddress("192.168.10.102/24") + # addr2 = IPAddress("192.168.10.103/24") + # + # ip.include_all?(addr1,addr2) + # #=> true + # + def include_all?(*others) + others.all? {|oth| include?(oth)} + end + + # + # True if the object is an IPv4 address + # + # ip = IPAddress("192.168.10.100/24") + # + # ip.ipv4? + # #-> true + # +# def ipv4? +# true +# end + + # + # True if the object is an IPv6 address + # + # ip = IPAddress("192.168.10.100/24") + # + # ip.ipv6? + # #-> false + # +# def ipv6? +# false +# end + + # # Checks if an IPv4 address objects belongs # to a private network RFC1918 # # Example: # @@ -941,22 +980,17 @@ end return dup.reverse end def aggregate(ip1,ip2) - if ip1.include? ip2 - return [ip1] + return [ip1] if ip1.include? ip2 + + snet = ip1.supernet(ip1.prefix-1) + if snet.include_all?(ip1, ip2) && ((ip1.size + ip2.size) == snet.size) + return [snet] else - snet = ip1.supernet(ip1.prefix-1) - arr1 = ip1.subnet(2**(ip2.prefix-ip1.prefix)).map{|i| i.to_string} - arr2 = snet.subnet(2**(ip2.prefix-snet.prefix)).map{|i| i.to_string} - if (arr2 - [ip2.to_string] - arr1).empty? - return [snet] - else - return [ip1, ip2] - end + return [ip1, ip2] end end - end # class IPv4 end # module IPAddress