Repository is archived
No commit activity in last 3 years
No release in over 3 years
A small gem that adds extra functionality to Rubys IPAddr class
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
 Project Readme
IPAddrExtensions
================

A selection of potentially useful extensions for IPAddr.

Example
=======

    >> IPAddr.new("192.0.2.0/24").length
    => 24
    >> IPAddr.new("192.0.2.0/24").first
    => #<IPAddr: IPv4:192.0.2.0/255.255.255.255>
    >> IPAddr.new("192.0.2.0/24").last
    => #<IPAddr: IPv4:192.0.2.255/255.255.255.255>
    >> IPAddr.new("192.0.2.0/24").scope
    => "DOCUMENTATION"
    >> IPAddr.new("192.0.2.0/24").local?
    => false
    >> IPAddr.new("192.0.2.0/24").unicast?
    => false
    >> IPAddr.new("192.0.2.0/24").multicast?
    => false
    >> IPAddr.new("192.0.2.0/24").link?
    => false
    >> IPAddr.new("192.0.2.0/24").documentation?
    => true
    >> IPAddr.new("192.0.2.0/24").loopback?
    => false
    >> IPAddr.new("192.0.2.0/24").global?
    => false
    >> IPAddr.new("192.0.2.0/24").private?
    => false
    >> IPAddr.new("192.0.2.0/24").space
    => 256
    >> IPAddr.new("192.0.2.0/24").reverses
    => ["192.0.2.in-addr.arpa"]
    >> IPAddr.new("192.0.2.0/24").host?
    => false
    >> IPAddr.new("192.0.2.0/24").prefix?
    => true
    >> IPAddr.new("2001:db8::/64").eui_64('00:0D:60:0F:3C:A8')
    => #<IPAddr: IPv6:2001:0db8:0000:0000:020d:60ff:fe0f:3ca8/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
    >> IPAddr.new("2001:db8::/56").reverses
    => ["0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "1.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "2.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "3.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "4.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "5.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "6.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "7.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "8.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "9.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "a.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "b.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "c.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "d.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "e.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa", "f.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa"]
    >> "192.0.2.0/24".to_ip
    => #<IPAddr: IPv4:192.0.2.0/255.255.255.0>
    >> 12345.to_ip
    => #<IPAddr: IPv4:0.0.48.57/255.255.255.255>
    >> 12345.to_ip(Socket::AF_INET6)
    => #<IPAddr: IPv6:0000:0000:0000:0000:0000:0000:0000:3039/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
    >> ("2001:db8::/32".to_ip / 16).collect { |prefix| prefix.to_string_including_length }
    => ["2001:db8::/36", "2001:db8:1000::/36", "2001:db8:2000::/36", "2001:db8:3000::/36", "2001:db8:4000::/36", "2001:db8:5000::/36", "2001:db8:6000::/36", "2001:db8:7000::/36", "2001:db8:8000::/36", "2001:db8:9000::/36", "2001:db8:a000::/36", "2001:db8:b000::/36", "2001:db8:c000::/36", "2001:db8:d000::/36", "2001:db8:e000::/36", "2001:db8:f000::/36"]
    => IPAddr.new("2001:db8::/32").subnets(35)
    => [#<IPAddr: IPv6:2001:0db8:0000:0000:0000:0000:0000:0000/ffff:ffff:e000:0000:0000:0000:0000:0000>, #<IPAddr: IPv6:2001:0db8:2000:0000:0000:0000:0000:0000/ffff:ffff:e000:0000:0000:0000:0000:0000>, #<IPAddr: IPv6:2001:0db8:4000:0000:0000:0000:0000:0000/ffff:ffff:e000:0000:0000:0000:0000:0000>, #<IPAddr: IPv6:2001:0db8:6000:0000:0000:0000:0000:0000/ffff:ffff:e000:0000:0000:0000:0000:0000>, #<IPAddr: IPv6:2001:0db8:8000:0000:0000:0000:0000:0000/ffff:ffff:e000:0000:0000:0000:0000:0000>, #<IPAddr: IPv6:2001:0db8:a000:0000:0000:0000:0000:0000/ffff:ffff:e000:0000:0000:0000:0000:0000>, #<IPAddr: IPv6:2001:0db8:c000:0000:0000:0000:0000:0000/ffff:ffff:e000:0000:0000:0000:0000:0000>, #<IPAddr: IPv6:2001:0db8:e000:0000:0000:0000:0000:0000/ffff:ffff:e000:0000:0000:0000:0000:0000>]


Copyright (c) 2010-2013 James Harton, Resistor Limited, released under the MIT License.