| Class | IP::V4 |
| In: |
lib/ip/base.rb
lib/ip/socket.rb |
| Parent: | IP |
| PROTO | = | "v4".freeze |
| ADDR_BITS | = | 32 |
| MASK | = | (1 << ADDR_BITS) - 1 |
| AF | = | Socket::AF_INET |
| orig_new | -> | new |
Parse a string; return an V4 instance if it‘s a valid IPv4 address, nil otherwise
# File lib/ip/base.rb, line 254
254: def self.parse(str)
255: if str =~ /\A(\d+)\.(\d+)\.(\d+)\.(\d+)(?:\/(\d+))?(?:@(.*))?\z/
256: pfxlen = ($5 || ADDR_BITS).to_i
257: return nil if pfxlen > 32
258: addrs = [$1.to_i, $2.to_i, $3.to_i, $4.to_i]
259: return nil if addrs.find { |n| n>255 }
260: addr = (((((addrs[0] << 8) | addrs[1]) << 8) | addrs[2]) << 8) | addrs[3]
261: new(addr, pfxlen, $6)
262: end
263: end