Class: IP::V4
Constant Summary collapse
- PROTO =
"v4".freeze
- ADDR_BITS =
32
- MASK =
(1 << ADDR_BITS) - 1
- AF =
Socket::AF_INET
Constants inherited from IP
Instance Attribute Summary
Attributes inherited from IP
Class Method Summary collapse
-
.parse(str) ⇒ Object
Parse a string; return an V4 instance if it’s a valid IPv4 address, nil otherwise.
Instance Method Summary collapse
-
#to_addr ⇒ Object
Return just the address part as a String in dotted decimal form.
Methods inherited from IP
#&, #+, #-, #<=>, #^, #af, #broadcast, #eql?, #freeze, from_cpal, #hash, #initialize, #inspect, #ipv4_compat?, #ipv4_mapped?, #mask, #mask!, #native, #netmask, #network, #offset, #offset?, orig_new, #proto, #reset_pfxlen!, #size, #succ, #succ!, #to_a, #to_addrlen, #to_ah, #to_cpal, #to_cphl, #to_hex, #to_i, #to_irange, #to_range, #to_s, #to_sockaddr, #wildmask, #|, #~
Constructor Details
This class inherits a constructor from IP
Class Method Details
.parse(str) ⇒ Object
Parse a string; return an V4 instance if it’s a valid IPv4 address, nil otherwise
273 274 275 276 277 278 279 280 281 282 |
# 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 |
Instance Method Details
#to_addr ⇒ Object
Return just the address part as a String in dotted decimal form
285 286 287 288 |
# 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 |