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.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/scriptroute/packets.rb', line 91 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.
134 135 136 |
# File 'lib/scriptroute/packets.rb', line 134 def <=>(other) @addr <=> other.to_i end |
#==(other) ⇒ Object
Compares numerically, for equality test.
138 139 140 |
# File 'lib/scriptroute/packets.rb', line 138 def ==(other) @addr == other.to_i end |
#eql?(other) ⇒ Boolean
in case you would like to store a Hash with IPaddresses as keys
130 131 132 |
# File 'lib/scriptroute/packets.rb', line 130 def eql?(other) @addr == other.to_i end |
#hash ⇒ Object
in case you would like to store a Hash with IPaddresses as keys
126 127 128 |
# File 'lib/scriptroute/packets.rb', line 126 def hash @addr end |
#inspect ⇒ String
just invokes to_s for now.
118 119 120 |
# File 'lib/scriptroute/packets.rb', line 118 def inspect to_s end |
#nameify ⇒ Object
142 143 144 |
# File 'lib/scriptroute/packets.rb', line 142 def nameify Scriptroute.nameify(self.to_s) end |
#to_i ⇒ Integer
122 123 124 |
# File 'lib/scriptroute/packets.rb', line 122 def to_i @addr end |
#to_s ⇒ String
Returns dotted quad notation of the IP address.
113 114 115 |
# File 'lib/scriptroute/packets.rb', line 113 def to_s [ @addr >> 24, @addr >> 16, @addr >> 8, @addr ].map { |i| i & 0xff }.join(".") end |