Skip to content

Commit

Permalink
Merge pull request #23 from beanieboi/ip-addr-cidr-method
Browse files Browse the repository at this point in the history
Add IPAddr.cidr to return ip address in cidr notation
  • Loading branch information
sorah committed Apr 10, 2024
2 parents b2bbf03 + f5b0067 commit db1b5e3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/ipaddr.rb
Expand Up @@ -227,6 +227,12 @@ def to_string
return str
end

# Returns a string containing the IP address representation in
# cidr notation
def cidr
format("%s/%s", to_s, prefix)
end

# Returns a network byte ordered string form of the IP address.
def hton
case @family
Expand Down
8 changes: 8 additions & 0 deletions test/test_ipaddr.rb
Expand Up @@ -20,18 +20,21 @@ def test_s_new
a = IPAddr.new
assert_equal("::", a.to_s)
assert_equal("0000:0000:0000:0000:0000:0000:0000:0000", a.to_string)
assert_equal("::/128", a.cidr)
assert_equal(Socket::AF_INET6, a.family)
assert_equal(128, a.prefix)

a = IPAddr.new("0123:4567:89ab:cdef:0ABC:DEF0:1234:5678")
assert_equal("123:4567:89ab:cdef:abc:def0:1234:5678", a.to_s)
assert_equal("0123:4567:89ab:cdef:0abc:def0:1234:5678", a.to_string)
assert_equal("123:4567:89ab:cdef:abc:def0:1234:5678/128", a.cidr)
assert_equal(Socket::AF_INET6, a.family)
assert_equal(128, a.prefix)

a = IPAddr.new("3ffe:505:2::/48")
assert_equal("3ffe:505:2::", a.to_s)
assert_equal("3ffe:0505:0002:0000:0000:0000:0000:0000", a.to_string)
assert_equal("3ffe:505:2::/48", a.cidr)
assert_equal(Socket::AF_INET6, a.family)
assert_equal(false, a.ipv4?)
assert_equal(true, a.ipv6?)
Expand All @@ -41,6 +44,7 @@ def test_s_new
a = IPAddr.new("3ffe:505:2::/ffff:ffff:ffff::")
assert_equal("3ffe:505:2::", a.to_s)
assert_equal("3ffe:0505:0002:0000:0000:0000:0000:0000", a.to_string)
assert_equal("3ffe:505:2::/48", a.cidr)
assert_equal(Socket::AF_INET6, a.family)
assert_equal(48, a.prefix)
assert_nil(a.zone_id)
Expand All @@ -58,12 +62,14 @@ def test_s_new
a = IPAddr.new("0.0.0.0")
assert_equal("0.0.0.0", a.to_s)
assert_equal("0.0.0.0", a.to_string)
assert_equal("0.0.0.0/32", a.cidr)
assert_equal(Socket::AF_INET, a.family)
assert_equal(32, a.prefix)

a = IPAddr.new("192.168.1.2")
assert_equal("192.168.1.2", a.to_s)
assert_equal("192.168.1.2", a.to_string)
assert_equal("192.168.1.2/32", a.cidr)
assert_equal(Socket::AF_INET, a.family)
assert_equal(true, a.ipv4?)
assert_equal(false, a.ipv6?)
Expand All @@ -72,13 +78,15 @@ def test_s_new
a = IPAddr.new("192.168.1.2/26")
assert_equal("192.168.1.0", a.to_s)
assert_equal("192.168.1.0", a.to_string)
assert_equal("192.168.1.0/26", a.cidr)
assert_equal(Socket::AF_INET, a.family)
assert_equal("#<IPAddr: IPv4:192.168.1.0/255.255.255.192>", a.inspect)
assert_equal(26, a.prefix)

a = IPAddr.new("192.168.1.2/255.255.255.0")
assert_equal("192.168.1.0", a.to_s)
assert_equal("192.168.1.0", a.to_string)
assert_equal("192.168.1.0/24", a.cidr)
assert_equal(Socket::AF_INET, a.family)
assert_equal(24, a.prefix)

Expand Down

0 comments on commit db1b5e3

Please sign in to comment.