Module: IpAddresslike

Included in:
IpAddress, IpNumeric
Defined in:
lib/gorillib/type/ip_address.rb

Constant Summary collapse

ONES =
0xFFFFFFFF

Instance Method Summary collapse

Instance Method Details

#bitness_max(bitness) ⇒ Object

Masks off all but the bitness most-significant-bits, filling with ones

Examples:

/24 fills the last quad

IpAddress.new('1.2.3.4').bitness_min(24) # '1.2.3.255'

Raises:



20
21
22
23
# File 'lib/gorillib/type/ip_address.rb', line 20

def bitness_max(bitness)
  raise ArgumentError, "IP addresses have only 32 bits (got #{bitness.inspect})" unless (0..32).include?(bitness)
  packed | (ONES >> bitness)
end

#bitness_min(bitness) ⇒ Object

Masks off all but the bitness most-significant-bits

Examples:

/24 keeps only the first three quads

IpAddress.new('1.2.3.4').bitness_min(24) # '1.2.3.0'

Raises:



9
10
11
12
13
# File 'lib/gorillib/type/ip_address.rb', line 9

def bitness_min(bitness)
  raise ArgumentError, "IP addresses have only 32 bits (got #{bitness.inspect})" unless (0..32).include?(bitness)
  lsbs = 32 - bitness
  (packed >> lsbs) << lsbs
end

#to_hexObject



25
26
27
# File 'lib/gorillib/type/ip_address.rb', line 25

def to_hex
  "%08x" % packed
end

#to_sObject



29
30
31
# File 'lib/gorillib/type/ip_address.rb', line 29

def to_s
  dotted
end