Class: IP::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/ip/range.rb

Overview

IP::Range - Calculates a range of IP addresses, and returns an Array of IP::Address objects.

Usage

IP::Range[‘10.0.0.1’, ‘10.0.0.2’] => IP::Address objects between 10.0.0.1 and 10.0.0.2 (inclusive)

IP::Range can also take two IP::Address objects.

Will throw a IP::AddressException if for some reason addresses cannot be parsed.

Class Method Summary collapse

Class Method Details

.[](addr1, addr2) ⇒ Object

See the documentation for IP::Range for more information on this method.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ip/range.rb', line 23

def self.[](addr1, addr2)
  raw1, raw2 = [nil, nil]
  tmpip = nil
  
  if addr1.kind_of? String
    addr1 = IP::Address::Util.string_to_ip(addr1)
  elsif ! addr1.kind_of? IP::Address
    raise IP::AddressException.new("IP Address is not type String or IP::Address")
  end
  
  if addr2.kind_of? String
    addr2 = IP::Address::Util.string_to_ip(addr2)
  elsif ! addr2.kind_of? IP::Address
    raise IP::AddressException.new("IP Address is not type String or IP::Address")
  end

  if addr2.class.name != addr1.class.name
    raise IP::AddressException.new("First and Second IP in range are not of the same type")
  end
  
  raw1 = addr1.pack
  raw2 = addr2.pack

  range = []
  
  # use the class we were given to force certain results, instead
  # of relying on the fairly inaccurate unpack facility.
  range = (raw1..raw2).collect { |x| addr1.class.new(x) }

  return range
end