Class: Scriptroute::IPaddress
- Inherits:
-
Object
- Object
- Scriptroute::IPaddress
- Defined in:
- lib/scriptroute/packets.rb
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares numerically, for sorting.
-
#==(other) ⇒ Object
Compares numerically, for equality test.
-
#eql?(other) ⇒ Boolean
in case you would like to store a Hash with IPaddresses as keys.
-
#hash ⇒ Object
in case you would like to store a Hash with IPaddresses as keys.
-
#initialize(n) ⇒ IPaddress
constructor
A new instance of IPaddress.
-
#inspect ⇒ String
just invokes to_s for now.
- #nameify ⇒ Object
- #to_i ⇒ Integer
-
#to_s ⇒ String
Dotted quad notation of the IP address.
Constructor Details
#initialize(n) ⇒ IPaddress
Returns a new instance of IPaddress.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/scriptroute/packets.rb', line 221 def initialize(n) raise "Seems bad to initialize an IPaddress with nil." unless n if(n.is_a?(String)) then shf = 32 if n =~ /^[\d\.]+$/ then @addr = n.split('.').map { |i| shf -= 8; i.to_i << shf }.sum else addr = Scriptroute::dns_lookup(n) if addr then @addr = addr.split('.').map { |i| shf -= 8; i.to_i << shf }.sum else $stderr.puts "unable to lookup #{n}" @addr = 0 end end elsif(n.is_a?(IPaddress)) then @addr = n.to_i else @addr = n end end |
Instance Method Details
#<=>(other) ⇒ Object
Compares numerically, for sorting.
264 265 266 |
# File 'lib/scriptroute/packets.rb', line 264 def <=>(other) @addr <=> other.to_i end |
#==(other) ⇒ Object
Compares numerically, for equality test.
268 269 270 |
# File 'lib/scriptroute/packets.rb', line 268 def ==(other) @addr == other.to_i end |
#eql?(other) ⇒ Boolean
in case you would like to store a Hash with IPaddresses as keys
260 261 262 |
# File 'lib/scriptroute/packets.rb', line 260 def eql?(other) @addr == other.to_i end |
#hash ⇒ Object
in case you would like to store a Hash with IPaddresses as keys
256 257 258 |
# File 'lib/scriptroute/packets.rb', line 256 def hash @addr end |
#inspect ⇒ String
just invokes to_s for now.
248 249 250 |
# File 'lib/scriptroute/packets.rb', line 248 def inspect to_s end |
#nameify ⇒ Object
272 273 274 |
# File 'lib/scriptroute/packets.rb', line 272 def nameify Scriptroute.nameify(self.to_s) end |
#to_i ⇒ Integer
252 253 254 |
# File 'lib/scriptroute/packets.rb', line 252 def to_i @addr end |
#to_s ⇒ String
Returns dotted quad notation of the IP address.
243 244 245 |
# File 'lib/scriptroute/packets.rb', line 243 def to_s [ @addr >> 24, @addr >> 16, @addr >> 8, @addr ].map { |i| i & 0xff }.join(".") end |