Class: IPAddress::Prefix32

Inherits:
Prefix
  • Object
show all
Defined in:
lib/ipaddress/prefix.rb

Constant Summary collapse

MAX =
32

Instance Attribute Summary

Attributes inherited from Prefix

#prefix

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Prefix

#+, #-, #<=>, #bits, #hash, #host_prefix, #initialize, #inspect, #max, #max?, #next, #prev, #subprefix, #superprefix, #to_i, #to_s, #validate_prefix

Methods included from Conversions

#addr2ary, #addr2bits, #ary2addr, #ary2data, #ary2int, #bits2addr, #data2ary, #data2bits, #data2int, #int2addr, #int2ary, #int2data

Constructor Details

This class inherits a constructor from IPAddress::Prefix

Class Method Details

.parse_netmask(netmask) ⇒ Object

Creates a new prefix by parsing a netmask in dotted decimal form

prefix = IPAddress::Prefix32.parse_netmask("255.255.255.0")
  #=> 24


191
192
193
194
# File 'lib/ipaddress/prefix.rb', line 191

def parse_netmask(netmask)
  octets = addr2ary(netmask)
  new(data2bits(octets.pack("C#{octets.size}")).count('1'))
end

Instance Method Details

#[](index) ⇒ Object Also known as: octet

Shortcut for the octecs in the dotted decimal representation

prefix = IPAddress::Prefix32.new(24)

prefix[2]
  #=> 255


235
236
237
# File 'lib/ipaddress/prefix.rb', line 235

def [](index)
  octets[index]
end

#hostmaskObject

The hostmask is the contrary of the subnet mask, as it shows the bits that can change within the hosts

prefix = IPAddress::Prefix32.new(24)

prefix.hostmask
  #=> "0.0.0.255"


251
252
253
# File 'lib/ipaddress/prefix.rb', line 251

def hostmask
  lazy_attr(:hostmask) { int2addr(~to_i) }
end

#octetsObject

An array of octets of the IPv4 dotted decimal format

prefix = IPAddress::Prefix32.new(24)

prefix.octets
  #=> [255, 255, 255, 0]


222
223
224
# File 'lib/ipaddress/prefix.rb', line 222

def octets
  lazy_attr(:octets) { addr2ary(to_ip) }
end

#to_ipObject

Gives the prefix in IPv4 dotted decimal format, i.e. the canonical netmask we’re all used to

prefix = IPAddress::Prefix32.new(24)

prefix.to_ip
  #=> "255.255.255.0"


209
210
211
# File 'lib/ipaddress/prefix.rb', line 209

def to_ip
  lazy_attr(:ip) { bits2addr(bits) }
end