class IPAddr
IPAddr provides a set of methods to manipulate an IP address. Both IPv4 and IPv6 are supported.
Example
require 'ipaddr' ipaddr1 = IPAddr.new "3ffe:505:2::1" p ipaddr1 #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0001/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff> p ipaddr1.to_s #=> "3ffe:505:2::1" ipaddr2 = ipaddr1.mask(48) #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0000/ffff:ffff:ffff:0000:0000:0000:0000:0000> p ipaddr2.to_s #=> "3ffe:505:2::" ipaddr3 = IPAddr.new "192.168.2.0/24" p ipaddr3 #=> #<IPAddr: IPv4:192.168.2.0/255.255.255.0>
Attributes
Returns the address family of this IP address.
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 67
def initialize: (?String addr, ?untyped family) -> void
Creates a new ipaddr object either from a human readable IP address representation in string, or from a packed in_addr value followed by an address family.
In the former case, the following are the valid formats that will be recognized: “address”, “address/prefixlen” and “address/mask”, where IPv6 address may be enclosed in square brackets ([' and ]‘). If a prefixlen or a mask is specified, it returns a masked IP address. Although the address family is determined automatically from a specified string, you can specify one explicitly by the optional second argument.
Otherwise an IP address is generated from a packed in_addr value and an address family.
The IPAddr class defines many methods and operators, and some of those, such as &, |, include? and ==, accept a string, or a packed in_addr value instead of an IPAddr object.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 33
def self.new_ntoh: (String addr) -> IPAddr
Creates a new ipaddr containing the given network byte ordered string form of an IP address.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 43
def self.ntop: (String addr) -> String
Convert a network byte ordered string form of an IP address into human readable form. It expects the string to be encoded in Encoding::ASCII_8BIT (BINARY).
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 75
def &: (untyped other) -> IPAddr
Returns a new ipaddr built by bitwise AND.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 83
def <<: (Integer num) -> IPAddr
Returns a new ipaddr built by bitwise left shift.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 91
def <=>: (untyped other) -> Integer?
Compares the ipaddr with another.
(untyped other) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 99
def ==: (untyped other) -> bool
Returns true if two ipaddrs are equal.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 114
def >>: (Integer num) -> IPAddr
Returns a new ipaddr built by bitwise right-shift.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 370
def |: (untyped other) -> IPAddr
Returns a new ipaddr built by bitwise OR.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 378
def ~: () -> IPAddr
Returns a new ipaddr built by bitwise negation.
(untyped other) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 122
def eql?: (untyped other) -> bool
Checks equality used by Hash.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 135
def hash: () -> Integer
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 143
def hton: () -> String
Returns a network byte ordered string form of the IP address.
(untyped other) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 162
def include?: (untyped other) -> bool
Returns true if the given ipaddr is in the range.
e.g.: require ‘ipaddr’ net1 = IPAddr.new(“192.168.2.0/24”) net2 = IPAddr.new(“192.168.2.100”) net3 = IPAddr.new(“192.168.3.0”) net4 = IPAddr.new(“192.168.2.0/16”) p net1.include?(net2) #=> true p net1.include?(net3) #=> false p net1.include?(net4) #=> false p net4.include?(net1) #=> true
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 171
def inspect: () -> String
Returns a string containing a human-readable representation of the ipaddr. (“#<IPAddr: family:address/mask>”)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 179
def ip6_arpa: () -> String
Returns a string for DNS reverse lookup compatible with RFC3172.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 187
def ip6_int: () -> String
Returns a string for DNS reverse lookup compatible with RFC1886.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 195
def ipv4?: () -> bool
Returns true if the ipaddr is an IPv4 address.
() → IPAddr
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 204
def ipv4_compat: () -> IPAddr
Returns a new ipaddr built by converting the native IPv4 address into an IPv4-compatible IPv6 address.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 212
def ipv4_compat?: () -> bool
Returns true if the ipaddr is an IPv4-compatible IPv6 address.
() → IPAddr
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 221
def ipv4_mapped: () -> IPAddr
Returns a new ipaddr built by converting the native IPv4 address into an IPv4-mapped IPv6 address.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 229
def ipv4_mapped?: () -> bool
Returns true if the ipaddr is an IPv4-mapped IPv6 address.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 237
def ipv6?: () -> bool
Returns true if the ipaddr is an IPv6 address.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 249
def link_local?: () -> bool
Returns true if the ipaddr is a link-local address. IPv4 addresses in 169.254.0.0/16 reserved by RFC 3927 and link-local IPv6 Unicast Addresses in fe80::/10 reserved by RFC 4291 are considered link-local. Link-local IPv4 addresses in the IPv4-mapped IPv6 address range are also considered link-local.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 258
def loopback?: () -> bool
Returns true if the ipaddr is a loopback address. Loopback IPv4 addresses in the IPv4-mapped IPv6 address range are also considered as loopback addresses.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 267
def mask: (String | Integer prefixlen) -> IPAddr
Returns a new ipaddr built by masking IP address with the given prefixlen/netmask. (e.g. 8, 64, “255.255.255.0”, etc.)
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 277
def native: () -> IPAddr
Returns a new ipaddr built by converting the IPv6 address into a native IPv4 address. If the IP address is not an IPv4-mapped or IPv4-compatible IPv6 address, returns self.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 285
def netmask: () -> String
Returns the netmask in string format e.g. 255.255.0.0
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 293
def prefix: () -> Integer
Returns the prefix length in bits for the ipaddr.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 301
def prefix=: (Integer prefix) -> self
Sets the prefix length in bits
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 313
def private?: () -> bool
Returns true if the ipaddr is a private address. IPv4 addresses in 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 as defined in RFC 1918 and IPv6 Unique Local Addresses in fc00::/7 as defined in RFC 4193 are considered private. Private IPv4 addresses in the IPv4-mapped IPv6 address range are also considered private.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 322
def reverse: () -> String
Returns a string for DNS reverse lookup. It returns a string in RFC3172 form for an IPv6 address.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 330
def succ: () -> IPAddr
Returns the successor to the ipaddr.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 338
def to_i: () -> Integer
Returns the integer representation of the ipaddr.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 346
def to_range: () -> Range[IPAddr]
Creates a Range object for the network address.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/ipaddr/0/ipaddr.rbs, line 354
def to_s: () -> String
Returns a string containing the IP address representation.