Class: IPAddress::Prefix128
Overview
class Prefix32 < Prefix
Instance Attribute Summary
Attributes inherited from Prefix
Instance Method Summary collapse
-
#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.
-
#initialize(num = 128) ⇒ Prefix128
constructor
Creates a new prefix object for 128 bits IPv6 addresses.
-
#to_u128 ⇒ Object
Unsigned 128 bits decimal number representing the prefix.
Methods inherited from Prefix
Constructor Details
#initialize(num = 128) ⇒ Prefix128
Creates a new prefix object for 128 bits IPv6 addresses
prefix = IPAddress::Prefix128.new 64
#=> 64
216 217 218 219 220 221 |
# File 'lib/ipaddress/prefix.rb', line 216 def initialize(num=128) unless (1..128).include? num.to_i raise ArgumentError, "Prefix must be in range 1..128, got: #{num}" end super(num.to_i) end |
Instance Method Details
#bits ⇒ Object
Transforms the prefix into a string of bits representing the netmask
prefix = IPAddress::Prefix128.new 64
prefix.bits
#=> "1111111111111111111111111111111111111111111111111111111111111111"
"0000000000000000000000000000000000000000000000000000000000000000"
233 234 235 |
# File 'lib/ipaddress/prefix.rb', line 233 def bits "1" * @prefix + "0" * (128 - @prefix) end |
#host_prefix ⇒ Object
Returns the length of the host portion of a netmask.
prefix = Prefix128.new 96
prefix.host_prefix
#=> 32
259 260 261 |
# File 'lib/ipaddress/prefix.rb', line 259 def host_prefix 128 - @prefix end |
#to_u128 ⇒ Object
Unsigned 128 bits decimal number representing the prefix
prefix = IPAddress::Prefix128.new 64
prefix.to_u128
#=> 340282366920938463444927863358058659840
246 247 248 |
# File 'lib/ipaddress/prefix.rb', line 246 def to_u128 bits.to_i(2) end |