Method: IPAddress::IPv4#split
- Defined in:
- lib/ipaddress/ipv4.rb
#split(subnets = 2) ⇒ Object Also known as: /
Splits a network into different subnets
If the IP Address is a network, it can be divided into multiple networks. If self is not a network, this method will calculate the network from the IP and then subnet it.
If subnets is an power of two number, the resulting networks will be divided evenly from the supernet.
network = IPAddress("172.16.10.0/24")
network / 4 # implies map{|i| i.to_string}
#=> ["172.16.10.0/26",
"172.16.10.64/26",
"172.16.10.128/26",
"172.16.10.192/26"]
If num is any other number, the supernet will be divided into some networks with a even number of hosts and other networks with the remaining addresses.
network = IPAddress("172.16.10.0/24")
network / 3 # implies map{|i| i.to_string}
#=> ["172.16.10.0/26",
"172.16.10.64/26",
"172.16.10.128/25"]
Returns an array of IPv4 objects
618 619 620 621 622 623 624 625 626 627 |
# File 'lib/ipaddress/ipv4.rb', line 618 def split(subnets=2) unless (1..(2**@prefix.host_prefix)).include? subnets raise ArgumentError, "Value #{subnets} out of range" end networks = subnet(newprefix(subnets)) until networks.size == subnets networks = sum_first_found(networks) end return networks end |