- Inheritance
- < Object
- Included Modules
- Comparable
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>
Constants
| Name | Description | |
|---|---|---|
| IN4MASK | = 0xffffffff | |
| IN6FORMAT | = (["%.4x"] * 8).join(':') | |
| IN6MASK | = 0xffffffffffffffffffffffffffffffff |
Attributes
| Name | Visibility | R/W | Description |
|---|---|---|---|
| family | public | R | Returns the address family of this IP address. |
Methods
Class
| Visibility | Signature |
|---|---|
| public | new (addr = '::', family = Socket::AF_UNSPEC) |
| public | new_ntoh (addr) |
| public | ntop (addr) |
Instance
| Visibility | Signature |
|---|---|
| public | & (other) |
| public | << (num) |
| public | <=> (other) |
| public | == (other) |
| public | === (other) |
| public | >> (num) |
| public | hton () |
| public | include? (other) |
| public | inspect () |
| public | ip6_arpa () |
| public | ip6_int () |
| public | ipv4? () |
| public | ipv4_compat () |
| public | ipv4_compat? () |
| public | ipv4_mapped () |
| public | ipv4_mapped? () |
| public | ipv6? () |
| public | mask (prefixlen) |
| public | native () |
| public | reverse () |
| public | succ () |
| public | to_i () |
| public | to_range () |
| public | to_s () |
| public | to_string () |
| public | | (other) |
| public | ~ () |
| protected | mask! (mask) |
| protected | set (addr, *family) |
Class Method Detail
new(addr = '::', family = Socket::AF_UNSPEC)
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 addess 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.
new_ntoh(addr)
Creates a new ipaddr containing the given network byte ordered string form of an IP address.
ntop(addr)
Convert a network byte ordered string form of an IP address into human readable form.
Instance Method Detail
&(other)
Returns a new ipaddr built by bitwise AND.
<<(num)
Returns a new ipaddr built by bitwise left shift.
<=>(other)
Compares the ipaddr with another.
==(other)
Returns true if two ipaddrs are equal.
===(other)
Alias for include?
>>(num)
Returns a new ipaddr built by bitwise right-shift.
hton()
Returns a network byte ordered string form of the IP address.
include?(other)
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")
p net1.include?(net2) #=> true
p net1.include?(net3) #=> false
inspect()
Returns a string containing a human-readable representation of the ipaddr. ("#<IPAddr: family:address/mask>")
ip6_arpa()
Returns a string for DNS reverse lookup compatible with RFC3172.
ip6_int()
Returns a string for DNS reverse lookup compatible with RFC1886.
ipv4?()
Returns true if the ipaddr is an IPv4 address.
ipv4_compat()
Returns a new ipaddr built by converting the native IPv4 address into an IPv4-compatible IPv6 address.
ipv4_compat?()
Returns true if the ipaddr is an IPv4-compatible IPv6 address.
ipv4_mapped()
ipv4_mapped?()
Returns true if the ipaddr is an IPv4-mapped IPv6 address.
ipv6?()
Returns true if the ipaddr is an IPv6 address.
mask(prefixlen)
Returns a new ipaddr built by masking IP address with the given prefixlen/netmask. (e.g. 8, 64, "255.255.255.0", etc.)
native()
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.
reverse()
Returns a string for DNS reverse lookup. It returns a string in RFC3172 form for an IPv6 address.
succ()
Returns the successor to the ipaddr.
to_i()
Returns the integer representation of the ipaddr.
to_range()
Creates a Range object for the network address.
to_s()
Returns a string containing the IP address representation.
to_string()
Returns a string containing the IP address representation in canonical form.
|(other)
Returns a new ipaddr built by bitwise OR.
~()
Returns a new ipaddr built by bitwise negation.