You can also just call standard's library IPAddr.new
that will parse subnets, IPV6 and other cool things: (IPAddr) and return nil
if the format was wrong.
Just do:
valid = !(IPAddr.new('192.168.2.0/24') rescue nil).nil?
#=> true
valid = !(IPAddr.new('192.168.2.256') rescue nil).nil?
#=> false