Skip to content
On this page

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class · Ruby 3.2

IPAddr

< Object include Comparable 46 instance methods 3 class methods

IPAddr provides a set of methods to manipulate an IP address. Both IPv4 and IPv6 are supported.

Example

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>

Class methods 3

Instance methods 46

Constants

32 bit mask for IPv4

Format string for IPv6

128 bit mask for IPv6

Regexp internally used for parsing IPv4 address.

Regexp internally used for parsing IPv6 address.

Regexp internally used for parsing IPv6 address.

No documentation available
Attributes
Read

Returns the address family of this IP address.