class IPAddress::Prefix

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.

Attributes

prefix[R]

Public Class Methods

new(num) click to toggle source

Creates a new general prefix

# File lib/ipaddress_2/prefix.rb, line 30
def initialize(num)
  @prefix = num.to_i
end

Public Instance Methods

+(oth) click to toggle source

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

# File lib/ipaddress_2/prefix.rb, line 78
def +(oth)
  if oth.is_a? Integer
    self.prefix + oth
  else
    x, y = oth.coerce(@prefix)
    x + y
  end
end
-(oth) click to toggle source

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

# File lib/ipaddress_2/prefix.rb, line 92
def -(oth)
  if oth.is_a? Integer
    self.prefix - oth
  else
    x, y = oth.coerce(@prefix)
    x - y
  end
end
<=>(oth) click to toggle source

Compare the prefix

# File lib/ipaddress_2/prefix.rb, line 65
def <=>(oth)
  if oth.is_a? Integer
    @prefix <=> oth
  else
    x, y = oth.coerce(@prefix)
    x <=> y
  end
end
coerce(other) click to toggle source

Provides support for Ruby type coercion

# File lib/ipaddress_2/prefix.rb, line 53
def coerce(other)
  case other
  when Integer
    [other, @prefix]
  else
    other.coerce(@prefix).reverse!
  end
end
inspect()
Alias for: to_s
to_i() click to toggle source

Returns the prefix

# File lib/ipaddress_2/prefix.rb, line 45
def to_i
  @prefix
end
Also aliased as: to_int
to_int()
Alias for: to_i
to_s() click to toggle source

Returns a string with the prefix

# File lib/ipaddress_2/prefix.rb, line 37
def to_s
  "#@prefix"
end
Also aliased as: inspect