Class: IPAddress::Prefix

Inherits:
Object
  • Object
show all
Includes:
Comparable
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

Constructor Details

#initialize(num) ⇒ Prefix

Creates a new general prefix



30
31
32
# File 'lib/ipaddress/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/prefix.rb', line 25

def prefix
  @prefix
end

Instance Method Details

#+(oth) ⇒ Object

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



60
61
62
63
64
65
66
# File 'lib/ipaddress/prefix.rb', line 60

def +(oth)
  if oth.is_a? Fixnum
    self.prefix + oth
  else
    self.prefix + oth.prefix
  end
end

#-(oth) ⇒ Object

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



73
74
75
76
77
78
79
# File 'lib/ipaddress/prefix.rb', line 73

def -(oth)
  if oth.is_a? Fixnum
    self.prefix - oth
  else
    (self.prefix - oth.prefix).abs
  end
end

#<=>(oth) ⇒ Object

Compare the prefix



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

def <=>(oth)
  @prefix <=> oth.to_i
end

#to_iObject

Returns the prefix



45
46
47
# File 'lib/ipaddress/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/prefix.rb', line 37

def to_s
  "#@prefix"
end