Class: IPAddress::Prefix

Inherits:
Object
  • Object
show all
Extended by:
Conversions
Includes:
Comparable, Conversions, Nuggets::LazyAttr
Defined in:
lib/ipaddress/prefix.rb

Overview

NAME

IPAddress::Prefix

SYNOPSIS

Parent class for Prefix32 and Prefix128

DESCRIPTION

IPAddress::Prefix is the parent class for IPAddress::Prefix32 and IPAddress::Prefix128, defining some modules in common for both the subclasses.

IPAddress::Prefix shouldn’t be accesses directly, unless for particular needs.

Direct Known Subclasses

Prefix128, Prefix32

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Conversions

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

Constructor Details

#initialize(num = nil) ⇒ Prefix

Creates a new prefix object for 32 bits IPv4 addresses / 128 bits IPv6 addresses.

prefix = IPAddress::Prefix32.new(24)
  #=> 24

prefix6 = IPAddress::Prefix128.new(64)
  #=> 64


40
41
42
43
44
45
46
# File 'lib/ipaddress/prefix.rb', line 40

def initialize(num = nil)
  validate_prefix(@prefix = case num
    when self.class then num.prefix
    when nil then max
    else num.to_i
  end)
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



48
49
50
# File 'lib/ipaddress/prefix.rb', line 48

def prefix
  @prefix
end

Instance Method Details

#+(other) ⇒ Object

Sums two prefixes or a prefix to a number, returns a Fixnum



115
116
117
# File 'lib/ipaddress/prefix.rb', line 115

def +(other)
  prefix + (other.is_a?(self.class) ? other.prefix : other)
end

#-(other) ⇒ Object

Returns the difference between two prefixes, or a prefix and a number, as a Fixnum



124
125
126
# File 'lib/ipaddress/prefix.rb', line 124

def -(other)
  (prefix - (other.is_a?(self.class) ? other.prefix : other)).abs
end

#<=>(other) ⇒ Object

Compare the prefix



101
102
103
# File 'lib/ipaddress/prefix.rb', line 101

def <=>(other)
  prefix <=> other.prefix
end

#bitsObject

Transforms the prefix into a string of bits representing the netmask

prefix32 = IPAddress::Prefix32.new(24)

prefix32.bits
  #=> "11111111111111111111111100000000"

prefix128 = IPAddress::Prefix128.new(64)

prefix128.bits
  #=> "1111111111111111111111111111111111111111111111111111111111111111"
      "0000000000000000000000000000000000000000000000000000000000000000"


76
77
78
# File 'lib/ipaddress/prefix.rb', line 76

def bits
  lazy_attr(:bits) { '1' * prefix << '0' * host_prefix }
end

#hashObject



105
106
107
# File 'lib/ipaddress/prefix.rb', line 105

def hash
  prefix.hash
end

#host_prefixObject

Returns the length of the host portion of a netmask.

prefix32 = Prefix32.new(24)

prefix32.host_prefix
  #=> 8

prefix128 = Prefix128.new(96)

prefix128.host_prefix
  #=> 32


142
143
144
# File 'lib/ipaddress/prefix.rb', line 142

def host_prefix
  lazy_attr(:host_prefix) { max - prefix }
end

#inspectObject



57
58
59
# File 'lib/ipaddress/prefix.rb', line 57

def inspect
  "#{self.class}@#{to_s}"
end

#maxObject



146
147
148
# File 'lib/ipaddress/prefix.rb', line 146

def max
  lazy_attr(:max) { self.class::MAX }
end

#max?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/ipaddress/prefix.rb', line 150

def max?
  lazy_attr(:max_p) { prefix == max }
end

#nextObject



158
159
160
# File 'lib/ipaddress/prefix.rb', line 158

def next
  lazy_attr(:next) { prefix + 1 unless max? }
end

#prevObject



154
155
156
# File 'lib/ipaddress/prefix.rb', line 154

def prev
  lazy_attr(:prev) { prefix - 1 }
end

#subprefix(num, relax = false) ⇒ Object



166
167
168
169
# File 'lib/ipaddress/prefix.rb', line 166

def subprefix(num, relax = false)
  validate_prefix(num, prefix, relax) or return
  [2 ** (max - num), 2 ** (num - prefix)]
end

#superprefix(num, relax = false) ⇒ Object



162
163
164
# File 'lib/ipaddress/prefix.rb', line 162

def superprefix(num, relax = false)
  validate_prefix([0, num].max, nil, prev, relax) or return
end

#to_iObject

Unsigned 32/128 bits decimal number representing the prefix

prefix = IPAddress::Prefix32.new(24)

prefix.to_u32
  #=> 4294967040

prefix6 = IPAddress::Prefix128.new(64)

prefix6.to_u128
  #=> 340282366920938463444927863358058659840


94
95
96
# File 'lib/ipaddress/prefix.rb', line 94

def to_i
  lazy_attr(:int) { bits.to_i(2) }
end

#to_sObject

Returns a string with the prefix



53
54
55
# File 'lib/ipaddress/prefix.rb', line 53

def to_s
  prefix.to_s
end

#validate_prefix(num, first = nil, last = nil, relax = false) ⇒ Object

Raises:

  • (ArgumentError)


171
172
173
174
# File 'lib/ipaddress/prefix.rb', line 171

def validate_prefix(num, first = nil, last = nil, relax = false)
  return num if (range = (first || 0)..(last || max)).include?(num)
  raise ArgumentError, "Prefix must be in range #{range}, got: #{num}" unless relax
end