Skip to content

Instantly share code, notes, and snippets.

@dallasmarlow
Created November 11, 2011 20:36
Show Gist options
  • Save dallasmarlow/1359167 to your computer and use it in GitHub Desktop.
Save dallasmarlow/1359167 to your computer and use it in GitHub Desktop.
# 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