Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,11 @@ def inspect
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
Expand Down
11 changes: 11 additions & 0 deletions test/test_ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ def test_to_s
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
Expand Down