Method: IPAddress#supernet

Defined in:
lib/ipaddress.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


1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
# File 'lib/ipaddress.rb', line 1353

def supernet(new_prefix)
  if (new_prefix >= @prefix.num)
    return nil
  end

  if new_prefix < 0
    new_prefix = 0
  end

  #  new_ip = @host_address.clone()
  #  for _ in [email protected] {
  #      new_ip = new_ip << 1
  #  }
  return from(@host_address, @prefix.from(new_prefix)).network()
end