module Net::IPAddress

Constants

MASK_SEPARATOR
V4
V6

@todo Implement :)

VERSION

Public Class Methods

for_i(int, mask_int=nil)
Alias for: for_integer
for_integer(int, mask_int=nil) click to toggle source

@param [Integer] int @return [IPAddress]

# File lib/net/ipaddress/singleton_class.rb, line 23
def for_integer(int, mask_int=nil)
  construct(__callee__, int, mask_int)
end
Also aliased as: for_i
new(octets, mask_octets=self.class::FULL_MASK) click to toggle source

Called “octets” is expected a FixedArray the 4 length, that's members 0~255 Fixnum. And the “mask_octets” is same format with the octets. @see octets, mask_octets

# File lib/net/ipaddress/instance.rb, line 12
def initialize(octets, mask_octets=self.class::FULL_MASK)
  raise InvalidAddress unless valid_octets?(octets)
  raise TypeError unless valid_octets?(mask_octets)

  @octets, @mask_octets = octets.dup.freeze, mask_octets.dup.freeze
end
parse(str) click to toggle source

@param [String] str @return [IPAddress]

# File lib/net/ipaddress/singleton_class.rb, line 9
def parse(str)
  construct(__callee__, str.to_str)
end
parse_big_endian(str) click to toggle source

@param [String] str @return [IPAddress]

# File lib/net/ipaddress/singleton_class.rb, line 15
def parse_big_endian(str)
  construct(__callee__, str.to_str)
end
Also aliased as: parse_byte_order
parse_byte_order(str)
Alias for: parse_big_endian
valid?(obj) click to toggle source

@abstract

# File lib/net/ipaddress/singleton_class.rb, line 30
def valid?(obj)
  case obj
  when String
    valid_string?(obj)
  when Integer
    valid_integer?(obj)
  else
    false
  end
end
valid_integer?(int) click to toggle source

@abstract

# File lib/net/ipaddress/singleton_class.rb, line 47
def valid_integer?(int)
  concrete_classies.any? { |klass| klass.__send__(__callee__, int) }
end
valid_string?(str) click to toggle source

@abstract

# File lib/net/ipaddress/singleton_class.rb, line 42
def valid_string?(str)
  concrete_classies.any? { |klass| klass.__send__(__callee__, str) }
end

Private Class Methods

concrete_classies() click to toggle source
# File lib/net/ipaddress/singleton_class.rb, line 53
def concrete_classies
  [Version4, Version6]
end
construct(constructor, *sources) click to toggle source
# File lib/net/ipaddress/singleton_class.rb, line 57
def construct(constructor, *sources)
  concrete_classies.each do |klass|
    begin
      instance = klass.__send__(constructor, *sources)
    rescue InvalidAddress
      next
    else
      return instance
    end
  end

  raise InvalidAddress
end

Public Instance Methods

<=>(other) click to toggle source

@return [0, 1, nil]

# File lib/net/ipaddress/instance.rb, line 58
def <=>(other)
  family?(other) ? (@octets <=> other.octets) : nil
end
==(other)
Alias for: eql?
===(other) click to toggle source

@return [Boolean]

# File lib/net/ipaddress/instance.rb, line 80
def ===(other)
  cover?(other)
end
addresses(&block)
Alias for: each_address
big_endian() click to toggle source

@return [String] @example

IPAddress('192.168.1.1').big_endian #=> "\xC0\xA8\x01\x01"
# File lib/net/ipaddress/instance.rb, line 87
def big_endian
  @octets.pack('C4')
end
Also aliased as: byte_order
bits() click to toggle source

@return [Array<Fixnum>] @example

IPAddress('192.168.1.1/24').bits
 #=> [1, 1, 0, 0, 0, 0, 0, 0,
      1, 0, 1, 0, 1, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 1,
      0, 0, 0, 0, 0, 0, 0, 1]
# File lib/net/ipaddress/instance.rb, line 44
def bits
  _bits.dup
end
byte_order()
Alias for: big_endian
bytes()
Alias for: octets
each_address(&block) click to toggle source

@return [self]

# File lib/net/ipaddress/instance.rb, line 126
def each_address(&block)
  return to_enum(__callee__) { subnet_counts } unless block

  to_range.__send__(:each, &block)
  self
end
Also aliased as: addresses
each_host(contain_network: false, &block) click to toggle source

@param contain_network [Boolean] @return [self]

# File lib/net/ipaddress/instance.rb, line 110
def each_host(contain_network: false, &block)
  return to_enum(__callee__, contain_network: contain_network) { host_counts + (contain_network ? 1 : 0) } unless block

  range = (contain_network ? network : network.next)...last
  range.__send__(:each, &block)
  self
end
Also aliased as: hosts
eql?(other) click to toggle source
# File lib/net/ipaddress/instance.rb, line 62
def eql?(other)
  family?(other) && (values == other.values)
end
Also aliased as: ==
freeze() click to toggle source

@return [self]

Calls superclass method
# File lib/net/ipaddress/instance.rb, line 74
def freeze
  memorize
  super
end
hash() click to toggle source

@return [Number]

# File lib/net/ipaddress/instance.rb, line 69
def hash
  values.hash
end
host_counts() click to toggle source

@return [Integer]

# File lib/net/ipaddress/instance.rb, line 104
def host_counts
  subnet_counts - 2
end
hosts(contain_network: false, &block)
Alias for: each_host
ipaddress?() click to toggle source
# File lib/net/ipaddress/instance.rb, line 48
def ipaddress?
  true
end
mask_octets() click to toggle source

@return [Array<Fixnum>] @example

IPAddress('192.168.1.1/24').mask_octets #=> [255, 255, 255, 0]
# File lib/net/ipaddress/instance.rb, line 31
def mask_octets
  @mask_octets.dup
end
Also aliased as: netmask
netmask()
Alias for: mask_octets
octets() click to toggle source

@return [Array<Fixnum>] @example

IPAddress('192.168.1.1/24').octets #=> [192, 168, 1 ,1]
# File lib/net/ipaddress/instance.rb, line 22
def octets
  @octets.dup
end
Also aliased as: bytes
space() click to toggle source

@return [Integer]

# File lib/net/ipaddress/instance.rb, line 99
def space
  subnet_counts - 1
end
subnet_counts() click to toggle source

@return [Integer]

# File lib/net/ipaddress/instance.rb, line 121
def subnet_counts
  2 ** (self.class.bit_length - prefix_length)
end
to_ipaddress() click to toggle source

@return [self]

# File lib/net/ipaddress/instance.rb, line 53
def to_ipaddress
  self
end
to_range() click to toggle source

@return [Range]

# File lib/net/ipaddress/instance.rb, line 94
def to_range
  network..last
end

Private Instance Methods

memorize() click to toggle source
# File lib/net/ipaddress/instance.rb, line 144
def memorize
  values
  _bits
  to_i
  nil
end
valid_octets?(octets) click to toggle source
# File lib/net/ipaddress/instance.rb, line 137
def valid_octets?(octets)
  (octets.length == self.class::FULL_MASK.length) &&
    octets.all? { |oct| (0..255).cover?(oct) && oct.integer? }
rescue NoMethodError
  false
end