Repository navigation
Conversation
Return the netmask string for a given prefix length and address
family, defaulting to IPv4:
IPAddr.netmask(24) #=> "255.255.255.0"
IPAddr.netmask(64, Socket::AF_INET6) #=> "ffff:ffff:ffff:ffff:0000:0000:0000:0000"
The prefix length must be an Integer; range and family validation are
delegated to IPAddr.new and IPAddr#mask.
Closes ruby#83.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
IPAddr.netmask(prefixlen, family = Socket::AF_INET), which returns the netmask string for a prefix length:Closes #83. Supersedes #117, which appears to be inactive. This version follows the review feedback given there:
IPAddr#netmask, with the address family passed as an argument instead of separateipv4_/ipv6_variants.IPAddr.new(0, family).mask(prefixlen).netmask, as suggested in Enhancement: add constants for IPv4 and IPv6 subnet masks #83 (comment).netmask?predicate.Details
prefixlenmust be anInteger; otherwiseInvalidPrefixErroris raised. Without this check,IPAddr#maskwould accept"24"and"255.255.255.0", raiseNoMethodErrorfornil, and return a non-contiguous mask for24.5.InvalidPrefixError) and family checks (AddressFamilyError) are left toIPAddr#maskandIPAddr.new.familydefaults toSocket::AF_INET.Tests
Added
test_class_netmask, covering IPv4/IPv6 boundary prefixes, the default family, non-Integer and out-of-range prefixes, andAF_UNSPEC.