Method: IPLogic::IP.wrap

Defined in:
lib/iplogic/ip.rb

.wrap(arg) ⇒ Object

Basic monad wrapper for IP addresses.

You can pass it:

a String >> IP(‘11.22.33.44’)

> #<IP [ 11.22.33.44 ]>

an Integer >> IP(0xFFFFFFFF)

> #<IP [ 255.255.255.255 ]>

an Array of int-ish objects >> IP([‘11’, 22, 33, ‘44’])

> #<IP [ 11.22.33.44 ]>

nil >> IP(nil)

> #<IP [ 0.0.0.0 ]>

an IP >> ip = IP(‘11.22.33.44’)

> #<IP [ 11.22.33.44 ]>

>> IP(ip)

> #<IP [ 11.22.33.44 ]>

>> ip.object_id == IP(ip).object_id

> true



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/iplogic/ip.rb', line 32

def wrap(arg)
  return arg if arg.is_a? IP

  int = case arg
  when Array
    parts_to_int(arg)
  when String
    parts_to_int(arg.split('.'))
  when Fixnum
    arg
  when nil
    0
  else
    raise FormatError, "IP: Unable to parse #{arg.inspect}"
  end

  return new(int)
end