Class: IPAddress::Prefix32
Constant Summary collapse
- IN4MASK =
0xffffffff
Instance Attribute Summary
Attributes inherited from Prefix
Class Method Summary collapse
-
.parse_netmask(netmask) ⇒ Object
Creates a new prefix by parsing a netmask in dotted decimal form.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Shortcut for the octecs in the dotted decimal representation.
-
#bits ⇒ Object
Transforms the prefix into a string of bits representing the netmask.
-
#host_prefix ⇒ Object
Returns the length of the host portion of a netmask.
-
#hostmask ⇒ Object
The hostmask is the contrary of the subnet mask, as it shows the bits that can change within the hosts.
-
#initialize(num) ⇒ Prefix32
constructor
Creates a new prefix object for 32 bits IPv4 addresses.
-
#octets ⇒ Object
An array of octets of the IPv4 dotted decimal format .
-
#to_ip ⇒ Object
Gives the prefix in IPv4 dotted decimal format, i.e.
-
#to_u32 ⇒ Object
Unsigned 32 bits decimal number representing the prefix.
Methods inherited from Prefix
#+, #-, #<=>, #coerce, #to_i, #to_s
Constructor Details
#initialize(num) ⇒ Prefix32
114 115 116 117 118 119 |
# File 'lib/ipaddress_2/prefix.rb', line 114 def initialize(num) unless (0..32).include? num raise ArgumentError, "Prefix must be in range 0..32, got: #{num}" end super(num) end |
Class Method Details
.parse_netmask(netmask) ⇒ Object
220 221 222 223 224 |
# File 'lib/ipaddress_2/prefix.rb', line 220 def self.parse_netmask(netmask) octets = netmask.split(".").map{|i| i.to_i} num = octets.pack("C"*octets.size).unpack("B*").first.count "1" return self.new(num) end |
Instance Method Details
#[](index) ⇒ Object
195 196 197 |
# File 'lib/ipaddress_2/prefix.rb', line 195 def [](index) octets[index] end |
#bits ⇒ Object
143 144 145 |
# File 'lib/ipaddress_2/prefix.rb', line 143 def bits "%.32b" % to_u32 end |
#host_prefix ⇒ Object
130 131 132 |
# File 'lib/ipaddress_2/prefix.rb', line 130 def host_prefix 32 - @prefix end |
#hostmask ⇒ Object
209 210 211 |
# File 'lib/ipaddress_2/prefix.rb', line 209 def hostmask [~to_u32].pack("N").unpack("CCCC").join(".") end |
#octets ⇒ Object
169 170 171 |
# File 'lib/ipaddress_2/prefix.rb', line 169 def octets to_ip.split(".").map{|i| i.to_i} end |
#to_ip ⇒ Object
156 157 158 |
# File 'lib/ipaddress_2/prefix.rb', line 156 def to_ip [bits].pack("B*").unpack("CCCC").join(".") end |