1

I am developing a Ruby on Rails application by using Ruby v1.8.7 . My question is more about Ruby language.

I know there is a build-in Ruby library named IPAddr in Ruby v1.8.7 . It handles IP address related issues well. But, seems it does not provide any method to check IP address format.

Could someone provide me a good way to check IP address format(IPv4 and IPv6 addresses) in Ruby? Thank you.

P.S.

I tried a regular expression way:

ip_regex = ^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.
([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$

ip_addr='127.0.0.3'
ip_addr =~ ip_regex

But it does not work...

Mellon
  • 37,586
  • 78
  • 186
  • 264
  • You need to fix your regular expression `ip_regex = /^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$/` – taro Dec 16 '11 at 10:34
  • @taro, can you answer the question instead of comment, so that I can accept your answer – Mellon Dec 16 '11 at 11:26

4 Answers4

6

http://www.ruby-doc.org/stdlib-1.8.7/libdoc/ipaddr/rdoc/IPAddr.html

The IPAddr class already provides methods to check whether they're IPV4 or IPV6 formats:

  • #ipv4?
  • #ipv6?
slm
  • 15,396
  • 12
  • 109
  • 124
Bryan
  • 3,220
  • 3
  • 26
  • 31
  • I'd recommend this method, it supports both IP4 and IP6 type addresses, and won't break on you in the future. – Peter Nov 16 '12 at 01:04
  • ActiveValidators, as mentioned by Damien, relies on IPAddr, so it's really up to whether or not you want to use a gem for that (which does plenty of other validations). – Franck Verrot Dec 02 '12 at 13:47
  • Something to keep in mind here is that those methods only work on an instance of the IPAddr class. So, if a non-IP value was suppplied like "300.300.300.300", you'd end up with an Argument error before you could use those methods. – claudijd Oct 30 '13 at 13:50
3

ActiveValidators has an IP validator.
It uses a regex for any ipv4, and Resolv::IPv6::Regex for any ipv6 address.

To use it, it's very simple.
Add activevalidators to your Gemfile.

gem 'activevalidators'

Then, in your Computer class :

class Computer << ActiveRecord::Base
  validates :remote_address,
    :ip => { :format => :v4 }
end
Damien MATHIEU
  • 31,924
  • 13
  • 86
  • 94
  • If I have a model class computer << ActiveRecord, how to use this validtor? – Mellon Dec 16 '11 at 10:25
  • Hi, I am developing a Rails v2.3.2 app. Seems the activevalidators only support Rails v3 ... Because I add the gem and run bundle install, it complains about my activerecord version – Mellon Dec 16 '11 at 11:09
  • Well, yeah, it is only rails3. You can still get the regex and use it in your rails2 app. Or (even better), upgrade ! – Damien MATHIEU Dec 17 '11 at 15:37
1

Looks like your regular expression is wrong. It should be:

/^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$/
taro
  • 5,772
  • 2
  • 30
  • 34
0
validates :ip,
      :format => {
        :with => Regexp.union(Resolv::IPv4::Regex, Resolv::IPv6::Regex)
      }

From https://stackoverflow.com/a/15157862/378006

wingfire
  • 791
  • 5
  • 9