[Solved] Count IP addresses


If you need to convert an IP to a range, you’ll need a function that converts an IPv4 value to an integer, then do math on those:

require 'ipaddr'

def ip(string)
  IPAddr.new(string).to_i
end

def parse_ip(string)
  string.split(/\s+\-\s+/).collect { |v| ip(v) }
end

def ip_range(string)
  ips = parse_ip(string)

  ips.last - ips.first + 1
end

ip_range("10.2.3.10 - 10.2.3.15")
# => 6

That should do it.

1

solved Count IP addresses