Method: IP::Address::Util.unpack

Defined in:
lib/ip/util.rb

.unpack(ip) ⇒ Object

Take an ‘Integer’ type and return an IP::Address object.

This routine will ‘guess’ at what version of IP addressing you want, returning the oldest type possible (IPv4 addresses will return IP::Address::IPv4 objects).

In almost all cases, you’ll want to use the type-specific routines, which merely involve passing an Integer to the constructor for each class.



47
48
49
50
51
52
53
54
55
56
# File 'lib/ip/util.rb', line 47

def unpack(ip)
  ret = raw_unpack(ip)

  # any IPv6 address should meet this criteria.
  if ret[2..8].any? { |x| x > 0 }
    return IP::Address::IPv6.new(ip)
  end

  return IP::Address::IPv4.new(ip)
end