Created
November 11, 2011 20:36
-
-
Save dallasmarlow/1359167 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ip.valid? '1.2.3.4' | |
module Ip | |
def self.pattern | |
@pattern ||= { | |
# pattern to match a single ip address or cidr | |
:ip => %r{^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(?:\/\d{1,2})?$}, | |
} | |
end | |
def self.valid? address | |
address.match(pattern[:ip]).captures.all? do |octet| | |
octet.to_i < 256 | |
end rescue false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment