Method: IPAddress::IPv4#subnet
- Defined in:
- lib/ipaddress/ipv4.rb
#subnet(subprefix) ⇒ Object
This method implements the subnetting function similar to the one described in RFC3531.
By specifying a new prefix, the method calculates the network number for the given IPv4 object and calculates the subnets associated to the new prefix.
For example, given the following network:
ip = IPAddress "172.16.10.0/24"
we can calculate the subnets with a /26 prefix
ip.subnets(26).map{&:to_string)
#=> ["172.16.10.0/26", "172.16.10.64/26",
"172.16.10.128/26", "172.16.10.192/26"]
The resulting number of subnets will of course always be a power of two.
682 683 684 685 686 687 688 689 |
# File 'lib/ipaddress/ipv4.rb', line 682 def subnet(subprefix) unless ((@prefix.to_i)..32).include? subprefix raise ArgumentError, "New prefix must be between #@prefix and 32" end Array.new(2**(subprefix-@prefix.to_i)) do |i| self.class.parse_u32(network_u32+(i*(2**(32-subprefix))), subprefix) end end |