class Pio::IPv4Address

IPv4 Address

Attributes

value[R]

@return [IPAddr] value object instance of proxied IPAddr.

Public Class Methods

new(addr) click to toggle source

Creates a {IPv4Address} instance object as a proxy to IPAddr class.

@overload initialize(addr)

@param [String|Number] addr

an IPv4 address specified either as a String or Number.

@raise [TypeError] invalid address if supplied argument is invalid

@return [IPv4Address] self

a proxy to IPAddr.
# File lib/pio/ipv4_address.rb, line 25
def initialize(addr)
  @value = case addr
           when Integer
             IPAddr.new(addr, Socket::AF_INET)
           when String
             IPAddr.new(addr)
           else
             addr.value
           end
end

Public Instance Methods

class_a?() click to toggle source

@return [bool]

Returns true if the address belongs to class A.
# File lib/pio/ipv4_address.rb, line 94
def class_a?
  mask(1).to_s == '0.0.0.0'
end
class_b?() click to toggle source

@return [bool]

Returns true if the address belongs to class B.
# File lib/pio/ipv4_address.rb, line 100
def class_b?
  mask(2).to_s == '128.0.0.0'
end
class_c?() click to toggle source

@return [bool]

Returns true if the address belongs to class C.
# File lib/pio/ipv4_address.rb, line 106
def class_c?
  mask(3).to_s == '192.0.0.0'
end
class_d?() click to toggle source

@return [bool]

Returns true if the address belongs to class D.
# File lib/pio/ipv4_address.rb, line 112
def class_d?
  mask(4).to_s == '224.0.0.0'
end
Also aliased as: multicast?
class_e?() click to toggle source

@return [bool]

Returns true if the address belongs to class E.
# File lib/pio/ipv4_address.rb, line 119
def class_e?
  mask(4).to_s == '240.0.0.0'
end
eql?(other) click to toggle source
# File lib/pio/ipv4_address.rb, line 47
def eql?(other)
  @value == other.value
end
hash() click to toggle source
# File lib/pio/ipv4_address.rb, line 51
def hash
  to_s.hash
end
mask(masklen) click to toggle source

Returns the IPv4 address masked with masklen. @return [IPv4Address]

# File lib/pio/ipv4_address.rb, line 87
def mask(masklen)
  clone.mask!(masklen)
end
Also aliased as: prefix
mask!(masklen) click to toggle source

Returns the IPv4 address masked with masklen. @return [IPv4Address]

# File lib/pio/ipv4_address.rb, line 79
def mask!(masklen)
  @value = @value.mask(masklen)
  self
end
Also aliased as: prefix!
multicast?()
Alias for: class_d?
prefix(masklen)
Alias for: mask
prefix!(masklen)
Alias for: mask!
prefixlen() click to toggle source

@return [Number] prefix length of IPv4 address.

# File lib/pio/ipv4_address.rb, line 56
def prefixlen
  netmask = to_range.first.to_i ^ to_range.last.to_i
  if netmask > 0
    32 - format('%b', netmask).length
  else
    32
  end
end
to_a() click to toggle source

@return [Array]

an array of decimal numbers converted from IPv4 address.
# File lib/pio/ipv4_address.rb, line 67
def to_a
  to_s.split('.').map(&:to_i)
end
to_ary() click to toggle source

@return [Array]

an array of decimal numbers converted from IPv4 address.
# File lib/pio/ipv4_address.rb, line 73
def to_ary
  to_a
end
unicast?() click to toggle source

@return [bool]

Returns true if the address is unicast address.
# File lib/pio/ipv4_address.rb, line 125
def unicast?
  class_a? || class_b? || class_c?
end