Method: IP::Address::IPv6#initialize

Defined in:
lib/ip/address.rb

#initialize(ip_address) ⇒ IPv6

Construct a new IP::Address::IPv6 object.

It can be passed two different types for construction:

  • A string which contains a valid, RFC4291-compliant IPv6 address

    (all forms are supported, including the
    backwards-compatibility IPv4 methods)
    
  • A 128-bit integer which is a sum of all the octets, left-most octet being the highest 32-bit portion (see IP::Address::Util for help generating this value)



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ip/address.rb', line 161

def initialize(ip_address)
  if ip_address.kind_of? Integer
    # unpack to generate a string, and parse that.
    # overwrites 'ip_address'
    # horribly inefficient, but general.

    raw = IP::Address::Util.raw_unpack(ip_address)

    ip_address = format_address(raw.reverse)
  end

  if ! ip_address.kind_of? String
    raise IP::AddressException.new("Fed IP address '#{ip_address}' is not String or Fixnum")
  end

  @ip_address = ip_address

  octets = parse_address(ip_address)

  if octets.length != 8
    raise IP::AddressException.new("IPv6 address '#{ip_address}' does not have 8 octets or a floating range specifier")
  end

  #
  # Now we check the contents of the address, to be sure we have
  # proper hexidecimal values
  #

  @octets = octets_atoi(octets)
end