From 74ed881e109ee2480854f5d47e8ef4a84b0cc1c5 Mon Sep 17 00:00:00 2001 From: Bogdan Irimie Date: Wed, 9 Oct 2019 13:23:27 +0300 Subject: [PATCH] [ruby/ipaddr] Add netmask method that returns net mask as string. https://github.com/ruby/ipaddr/commit/283d16f3a3 --- lib/ipaddr.rb | 5 +++++ test/test_ipaddr.rb | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb index 6c0ded2a77..c21c0cbf12 100644 --- a/lib/ipaddr.rb +++ b/lib/ipaddr.rb @@ -466,6 +466,11 @@ class IPAddr af, _to_string(@addr), _to_string(@mask_addr)) end + # Returns the netmask in string format e.g. 255.255.0.0 + def netmask + _to_string(@mask_addr) + end + protected # Set +@addr+, the internal stored ip address, to given +addr+. The diff --git a/test/test_ipaddr.rb b/test/test_ipaddr.rb index 2b6e5c7524..c055f4b2c4 100644 --- a/test/test_ipaddr.rb +++ b/test/test_ipaddr.rb @@ -220,6 +220,17 @@ class TC_IPAddr < Test::Unit::TestCase assert_equal("3ffe:0505:0002:0000:0000:0000:0000:0001", IPAddr.new("3ffe:505:2::1").to_string) assert_equal("3ffe:505:2::1", IPAddr.new("3ffe:505:2::1").to_s) end + + def test_netmask + a = IPAddr.new("192.168.1.2/8") + assert_equal(a.netmask, "255.0.0.0") + + a = IPAddr.new("192.168.1.2/16") + assert_equal(a.netmask, "255.255.0.0") + + a = IPAddr.new("192.168.1.2/24") + assert_equal(a.netmask, "255.255.255.0") + end end class TC_Operator < Test::Unit::TestCase