Class: IPAddress::Prefix

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/ipaddress_2/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

Constructor Details

#initialize(num) ⇒ Prefix

Creates a new general prefix



30
31
32
# File 'lib/ipaddress_2/prefix.rb', line 30

def initialize(num)
  @prefix = num.to_i
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



25
26
27
# File 'lib/ipaddress_2/prefix.rb', line 25

def prefix
  @prefix
end

Instance Method Details

#+(oth) ⇒ Object

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



78
79
80
81
82
83
84
85
# 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) ⇒ Object

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



92
93
94
95
96
97
98
99
# 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) ⇒ Object

Compare the prefix



65
66
67
68
69
70
71
72
# 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) ⇒ Object

Provides support for Ruby type coercion



53
54
55
56
57
58
59
60
# File 'lib/ipaddress_2/prefix.rb', line 53

def coerce(other)
  case other
  when Integer
    [other, @prefix]
  else
    other.coerce(@prefix).reverse!
  end
end

#to_iObject Also known as: to_int

Returns the prefix



45
46
47
# File 'lib/ipaddress_2/prefix.rb', line 45

def to_i
  @prefix
end

#to_sObject Also known as: inspect

Returns a string with the prefix



37
38
39
# File 'lib/ipaddress_2/prefix.rb', line 37

def to_s
  "#@prefix"
end