Class: IpRanges::Ip
- Inherits:
-
Object
- Object
- IpRanges::Ip
- Defined in:
- lib/ip-ranges/ip.rb
Overview
Class to represent a single IP number. Exposes methods for comparisons with other IP numbers, as well as a method to increment the IP number.
Instance Attribute Summary collapse
-
#number ⇒ Object
String representation of this Ip number.
Instance Method Summary collapse
-
#==(ip) ⇒ Object
Arguments:.
-
#>(ip) ⇒ Object
Arguments:.
-
#>=(ip) ⇒ Object
Arguments:.
-
#decrement ⇒ Object
Changes the ‘number’ property of this Ip to the number before this one.
-
#increment ⇒ Object
Changes the ‘number’ property of this Ip to the next number after this one.
-
#initialize(params = {}) ⇒ Ip
constructor
Arguments:.
-
#to_s ⇒ Object
Example: Ip.new(:number => ‘1.2.3.4’).to_s => ‘1.2.3.4’.
Constructor Details
Instance Attribute Details
Instance Method Details
#==(ip) ⇒ Object
45 46 47 |
# File 'lib/ip-ranges/ip.rb', line 45 def ==(ip) number == ip.number end |
#>(ip) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ip-ranges/ip.rb', line 78 def >(ip) a1, b1, c1, d1 = tuples number a2, b2, c2, d2 = tuples ip.number if a1 > a2 true elsif a1 < a2 false elsif b1 > b2 true elsif b1 < b2 false elsif c1 > c2 true elsif c1 < c2 false else d1 > d2 end end |
#>=(ip) ⇒ Object
62 63 64 |
# File 'lib/ip-ranges/ip.rb', line 62 def >=(ip) self == ip || self > ip end |
#decrement ⇒ Object
Changes the ‘number’ property of this Ip to the number before this one.
example:
ip = Ip.new(:number => '1.2.3.4')
ip.number => '1.2.3.4'
ip.decrement
ip.number => '1.2.3.3'
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ip-ranges/ip.rb', line 108 def decrement a, b, c, d = tuples number if d > 0 d -= 1 else d = 255 if c > 0 c -= 1 else c = 255 if b > 0 b -= 1 else b = 255 if a > 0 a -= 1 else raise "No more IPs" end end end end @number = [a, b, c, d].join('.') end |
#increment ⇒ Object
Changes the ‘number’ property of this Ip to the next number after this one.
example:
ip = Ip.new(:number => '1.2.3.4')
ip.number => '1.2.3.4'
ip.increment
ip.number => '1.2.3.5'
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/ip-ranges/ip.rb', line 143 def increment a, b, c, d = tuples number if d < 255 d += 1 else d = 0 if c < 255 c += 1 else c = 0 if b < 255 b += 1 else b = 0 if a < 255 a += 1 else raise "No more IPs" end end end end @number = [a, b, c, d].join('.') end |
#to_s ⇒ Object
Example: Ip.new(:number => ‘1.2.3.4’).to_s => ‘1.2.3.4’
29 30 31 |
# File 'lib/ip-ranges/ip.rb', line 29 def to_s number end |