Class: IPAddress::Crunchy

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/ipaddress/crunchy.rb

Constant Summary collapse

ZERO =
Crunchy.from_number(0)
ONE =
Crunchy.from_number(1)
TWO =
Crunchy.from_number(2)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCrunchy

Returns a new instance of Crunchy.



7
8
9
# File 'lib/ipaddress/crunchy.rb', line 7

def initialize()
  @num = 0
end

Instance Attribute Details

#numObject

Returns the value of attribute num.



6
7
8
# File 'lib/ipaddress/crunchy.rb', line 6

def num
  @num
end

Class Method Details

.from_number(val) ⇒ Object



29
30
31
32
33
# File 'lib/ipaddress/crunchy.rb', line 29

def self.from_number(val)
  ret = Crunchy.new()
  ret.num = val
  return ret
end

.from_string(val, radix) ⇒ Object



35
36
37
38
39
# File 'lib/ipaddress/crunchy.rb', line 35

def self.from_string(val, radix)
  ret = Crunchy.new()
  ret.num = val.to_i(radix)
  return ret
end

.oneObject



122
123
124
# File 'lib/ipaddress/crunchy.rb', line 122

def self.one
  return ONE
end

.parse(val) ⇒ Object



25
26
27
# File 'lib/ipaddress/crunchy.rb', line 25

def self.parse(val)
  return self.from_string(val, 10)
end

.twoObject



127
128
129
# File 'lib/ipaddress/crunchy.rb', line 127

def self.two
  return TWO
end

.zeroObject



117
118
119
# File 'lib/ipaddress/crunchy.rb', line 117

def self.zero
  return ZERO
end

Instance Method Details

#<=>(oth) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ipaddress/crunchy.rb', line 11

def <=>(oth)
  if oth.instance_of?(Crunchy)
    @num <=> oth.num
  else
    @num <=> oth
  end
end

#add(y) ⇒ Object



65
66
67
68
69
# File 'lib/ipaddress/crunchy.rb', line 65

def add(y)
  ret = Crunchy.new
  ret.num = self.num + y.num
  return ret
end

#cloneObject



19
20
21
22
23
# File 'lib/ipaddress/crunchy.rb', line 19

def clone()
  ret = Crunchy.new()
  ret.num = @num
  return ret
end

#compare(y) ⇒ Object



41
42
43
# File 'lib/ipaddress/crunchy.rb', line 41

def compare(y)
  return self.num <=> y.num
end

#div(y) ⇒ Object



95
96
97
98
99
# File 'lib/ipaddress/crunchy.rb', line 95

def div(y)
  ret = Crunchy.new
  ret.num = self.num / y.num
  return ret
end

#eq(oth) ⇒ Object



45
46
47
# File 'lib/ipaddress/crunchy.rb', line 45

def eq(oth)
  return self.compare(oth) == 0
end

#gt(oth) ⇒ Object



57
58
59
# File 'lib/ipaddress/crunchy.rb', line 57

def gt(oth)
  return self.compare(oth) > 0
end

#gte(oth) ⇒ Object



61
62
63
# File 'lib/ipaddress/crunchy.rb', line 61

def gte(oth)
  return self.compare(oth) >= 0
end

#lt(oth) ⇒ Object



53
54
55
# File 'lib/ipaddress/crunchy.rb', line 53

def lt(oth)
  return self.compare(oth) < 0
end

#lte(oth) ⇒ Object



49
50
51
# File 'lib/ipaddress/crunchy.rb', line 49

def lte(oth)
  return self.compare(oth) <= 0
end

#mds(y) ⇒ Object



107
108
109
110
# File 'lib/ipaddress/crunchy.rb', line 107

def mds(y)
  ret = Crunchy.new
  self.num % y
end

#mod(y) ⇒ Object



101
102
103
104
105
# File 'lib/ipaddress/crunchy.rb', line 101

def mod(y)
  ret = Crunchy.new
  ret.num = self.num % y.num
  return ret
end

#mul(y) ⇒ Object



77
78
79
80
81
# File 'lib/ipaddress/crunchy.rb', line 77

def mul(y)
  ret = Crunchy.new
  ret.num = self.num * y.num
  return ret
end

#shl(s) ⇒ Object



89
90
91
92
93
# File 'lib/ipaddress/crunchy.rb', line 89

def shl(s)
  ret = Crunchy.new
  ret.num = self.num << s
  return ret
end

#shr(s) ⇒ Object



83
84
85
86
87
# File 'lib/ipaddress/crunchy.rb', line 83

def shr(s)
  ret = Crunchy.new
  ret.num = self.num >> s
  return ret
end

#sub(y) ⇒ Object



71
72
73
74
75
# File 'lib/ipaddress/crunchy.rb', line 71

def sub(y)
  ret = Crunchy.new
  ret.num = self.num - y.num
  return ret
end

#toString(radix = 10) ⇒ Object



112
113
114
# File 'lib/ipaddress/crunchy.rb', line 112

def toString(radix=10)
  return self.num.to_s(radix)
end