Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class IP
- # Class method containsIP first checks for a valid IP address in the string
- # and then creates an instance of IP
- def self.containsIP?(ipaddress)
- if (ipaddress.is_a?(String))
- # Search the string for regular IP address pattern and convert result into
- # an array
- m = ipaddress.match(/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/).to_a
- # Delete the ipaddress from the resulting array
- m.delete(ipaddress)
- # Convert the string literals into integers
- i = Array.new
- m.each { |s| i << s.to_i }
- if (i.size == 4)
- # a, b, d and d all in valid range?
- if (i - (0..255).to_a).empty?
- new(i[0], i[1], i[2], i[3])
- else
- puts "IP is not in valid range."
- end
- else
- puts "Did not find a valid IP address in #{ipaddress}"
- end
- else
- puts "Error: I expected a string containing an IP address."
- end
- end
- def initialize(a, b, c, d)
- @a = a
- @b = b
- @c = c
- @d = d
- end
- def to_s
- "#{@a}.#{@b}.#{@c}.#{@d}"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement