Class IPAddr
In: lib/ipaddr.rb
Parent: Object

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>

Methods

&   <<   ==   ===   >>   hton   include?   inspect   ip6_arpa   ip6_int   ipv4?   ipv4_compat   ipv4_compat?   ipv4_mapped   ipv4_mapped?   ipv6?   mask   mask!   native   new   new_ntoh   ntop   reverse   set   to_i   to_s   to_string   |   ~  

Constants

IN4MASK = 0xffffffff
IN6MASK = 0xffffffffffffffffffffffffffffffff
IN6FORMAT = (["%.4x"] * 8).join(':')

Attributes

family  [R]  Returns the address family of this IP address.

Public Class methods

Creates a new ipaddr containing the given human readable form of an IP address. It also accepts `address/prefixlen’ and `address/mask’. When prefixlen or mask is specified, it returns a masked ipaddr. IPv6 address may beenclosed with `[’ and `]’.

Although an address family is determined automatically from a specified address, you can specify an address family explicitly by the optional second argument.

Creates a new ipaddr containing the given network byte ordered string form of an IP address.

Convert a network byte ordered string form of an IP address into human readable form.

Public Instance methods

Returns a new ipaddr built by bitwise AND.

Returns a new ipaddr built by bitwise left shift.

Returns true if two ipaddr are equal.

===(other)

Alias for include?

Returns a new ipaddr built by bitwise right-shift.

Returns a network byte ordered string form of the IP address.

Returns true if the given ipaddr is in the range.

e.g.:

  require 'ipaddr'
  net1 = IPAddr.new("192.168.2.0/24")
  p net1.include?(IPAddr.new("192.168.2.0"))        #=> true
  p net1.include?(IPAddr.new("192.168.2.255"))      #=> true
  p net1.include?(IPAddr.new("192.168.3.0"))        #=> false

Returns a string containing a human-readable representation of the ipaddr. ("#<IPAddr: family:address/mask>")

Returns a string for DNS reverse lookup compatible with RFC3172.

Returns a string for DNS reverse lookup compatible with RFC1886.

Returns true if the ipaddr is an IPv4 address.

Returns a new ipaddr built by converting the native IPv4 address into an IPv4-compatible IPv6 address.

Returns true if the ipaddr is an IPv4-compatible IPv6 address.

Returns a new ipaddr built by converting the native IPv4 address into an IPv4-mapped IPv6 address.

Returns true if the ipaddr is an IPv4-mapped IPv6 address.

Returns true if the ipaddr is an IPv6 address.

Returns a new ipaddr built by masking IP address with the given prefixlen/netmask. (e.g. 8, 64, "255.255.255.0", etc.)

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.

Returns a string for DNS reverse lookup. It returns a string in RFC3172 form for an IPv6 address.

Returns the integer representation of the ipaddr.

Returns a string containing the IP address representation.

Returns a string containing the IP address representation in canonical form.

Returns a new ipaddr built by bitwise OR.

Returns a new ipaddr built by bitwise negation.

Protected Instance methods

[Validate]

ruby-doc.org is a service of James Britt and Neurogami, a Ruby application development company in Phoenix, AZ.

Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.

For more information on the Ruby programming language, visit ruby-lang.org.

Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.