Parent

Methods

Files

Class/Module Index [+]

Quicksearch

IP::V4

Constants

ADDR_BITS
AF
MASK
PROTO

Public Class Methods

parse(str) click to toggle source

Parse a string; return an V4 instance if it’s a valid IPv4 address, nil otherwise

# File lib/ip/base.rb, line 273
def self.parse(str)
  if str =~ /\A(\d+)\.(\d+)\.(\d+)\.(\d+)(?:\/(\d+))?(?:@(.*))?\z/
    pfxlen = ($5 || ADDR_BITS).to_i
    return nil if pfxlen > 32
    addrs = [$1.to_i, $2.to_i, $3.to_i, $4.to_i]
    return nil if addrs.find { |n| n>255 }
    addr = (((((addrs[0] << 8) | addrs[1]) << 8) | addrs[2]) << 8) | addrs[3]
    new(addr, pfxlen, $6)
  end
end

Public Instance Methods

to_addr() click to toggle source

Return just the address part as a String in dotted decimal form

# File lib/ip/base.rb, line 285
def to_addr
  sprintf("%d.%d.%d.%d",
    (@addr>>24)&0xff, (@addr>>16)&0xff, (@addr>>8)&0xff, @addr&0xff)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.