IP address manipulation library
Latest commit 8e71d7f Jan 23, 2015 @deploy2 Merge pull request #18 from chrismar035/patch-1
Add license to gemspec
Failed to load latest commit information.
lib Factor out the ARPA suffix into a constant Oct 3, 2014
test Put assert_equal the right way round Oct 3, 2014
.travis.yml Update Gemfile to reference gemspec. Add development dependencies to … Aug 31, 2014
COPYING.txt Add distribution terms Jan 26, 2010
Gemfile Update Gemfile to reference gemspec. Add development dependencies to … Aug 31, 2014
LICENSE.txt Add distribution terms Jan 26, 2010
README.rdoc updated readme Nov 14, 2013
Rakefile Assorted changes to reduce the number of Rubocop violations. No chang… Sep 5, 2014
ruby-ip.gemspec Add license to gemspec Jan 22, 2015


ruby-ip library

This is a library for manipulating IP addresses.



gem install ruby-ip


git clone git://github.com/deploy2/ruby-ip.git



Feature overview

  • Create from string and to string

    require 'ip'
    ip = IP.new("")
    ip.to_s       # ""
    ip.to_i       # 3221226037
    ip.to_b       # 11000000000000000000001000110101
    ip.to_hex     # "c0000235"
    ip.to_addr    # ""
    ip.to_arpa    # ""
    ip.pfxlen     # 24
  • Qualify IP address with “routing context” (VRF)

    ip = IP.new("")
    ip.to_s       # ""
    ip.to_addrlen # ""
    ip.ctx        # "cust1"
  • Clean implementation of IP::V4 and IP::V6 as subclasses of IP

    ip1 = IP.new("")       #<IP::V4>
    ip2 = IP.new("2001:db8:be00::/48")  #<IP::V6 2001:db8:be00::/48>
    ip1.is_a?(IP::V4)                   # true
  • Create directly from integers or hex

    ip = IP::V4.new(3221226037, 24, "cust1")
    ip = IP::V4.new("c0000235", 24, "cust1")
  • Netmask manipulation

    ip = IP.new("")
    ip.network         #<IP::V4>
    ip.network(1)      #<IP::V4>
    ip.broadcast       #<IP::V4>
    ip.broadcast(-1)   #<IP::V4>
    ip.mask            # 255
    ip.size            # 256
    ip.netmask.to_s    # ""
    ip.wildmask.to_s   # ""
  • Address masking

    ip = IP.new("")
    ip.offset?         # true
    ip.offset          # 53
    ip.to_s            # ""
    ip.offset?         # false
  • Simple IP arithmetic

    ip = IP.new("")
    ip + 4             #<IP::V4>
    ip | 7             #<IP::V4>
    ip ^ 7             #<IP::V4>
    ~ip                #<IP::V4>
  • Advanced Subnet Operations

    sn = IP.new('')
    ip = IP.new('')
    sn.split                    [#<IP::V4>, 
                                #<IP::V4>] (2 evenly divided subnets)
    sn.divide_by_subnets(3)     [#<IP::V4>, 
                                #<IP::V4>] (4 evenly divided subnets)
    #keep in mind this always takes into account a network and broadcast address
    sn.divide_by_hosts(100)     [#<IP::V4>, 
                                #<IP::V4>] (128 hosts each)
    ip = IP.new('')
    => true
  • Convert to and from a compact Array representation

    ip1 = IP.new("")
    ip1.to_a     # ["v4", 3221226037, 24, "cust1"]
    ip2 = IP.new(["v4", 3221226037, 24, "cust1"])
    ip1 == ip2   # true
  • Hex array representation, useful when talking to languages which don't have Bignum support

    ip1 = IP.new("2001:db8:be00::/48@cust1")
    ip1.to_ah    # ["v6", "20010db8be0000000000000000000000", 48, "cust1"]
    ip2 = IP.new(["v6", "20010db8be0000000000000000000000", 48, "cust1"])
    ip1 == ip2   # true
  • Addresses are Comparable, sortable, and can be used as Hash keys

Why not IPAddr?

Ruby bundles an IPAddr class (ipaddr.rb). However there are a number of serious problems with this library.

  1. Given an IP address with a netmask or prefix (e.g. it's very hard to get access to the netmask part. It involves digging around instance variables.

  2. It's impossible to deal with an off-base address with prefix, because IPAddr forcibly masks it to the base. e.g. is stored as

  3. IPAddr uses calls to the socket library to validate IP addresses, and this can trigger spurious DNS lookups when given an invalid IP address. ruby-ip does not depend on the socket library at all, unless you require 'ip/socket' to have access to the Socket::AF_INET and Socket::AF_INET6 constants.


You may use, distribute and modify this software under the same terms as ruby itself (see LICENSE.txt and COPYING.txt)