Method: IPAddress::IPv4#supernet

Defined in:
lib/ipaddress/ipv4.rb

#supernet(new_prefix) ⇒ Object

Returns a new IPv4 object from the supernetting of the instance network.

Supernetting is similar to subnetting, except that you getting as a result a network with a smaller prefix (bigger host space). For example, given the network

ip = IPAddress("172.16.10.0/24")

you can supernet it with a new /23 prefix

ip.supernet(23).to_string
  #=> "172.16.10.0/23"

However if you supernet it with a /22 prefix, the network address will change:

ip.supernet(22).to_string
  #=> "172.16.8.0/22"

If new_prefix is less than 1, returns 0.0.0.0/0

Raises:

  • (ArgumentError)


767
768
769
770
771
# File 'lib/ipaddress/ipv4.rb', line 767

def supernet(new_prefix)
  raise ArgumentError, "New prefix must be smaller than existing prefix" if new_prefix >= @prefix.to_i
  return self.class.new("0.0.0.0/0") if new_prefix < 1
  return self.class.new(@address+"/#{new_prefix}").network
end