Repository navigation
Conversation
|
@taketo1113 san |
Equivalents for #83 examplesWith this change, the examples proposed in #83 can be written as follows:
|
Add a class method to generate a netmask string from a prefix length and address family. Examples: IPAddr.netmask(24, Socket::AF_INET) # => "255.255.255.0" IPAddr.netmask(64, Socket::AF_INET6) # => "ffff:ffff:ffff:ffff:0000:0000:0000:0000"
75c69b7 to
2b3d6d8
Compare
taketo1113
left a comment
There was a problem hiding this comment.
@PharaohKJ Thank you for the update.
I have made a few minor changes to the documentation and tests, and I will merge it as is.
I have also updated the pull request title so it can be included in the CHANGELOG.
Summary
IPAddr.netmask(prefix, family)to generate IPv4 and IPv6 subnet mask strings on demand, avoiding load-time memory overhead from precomputed tables.IPAddr#maskandIPAddr#netmaskfor a minimal implementation.What changed
Added
IPAddr.netmask(prefix, family = Socket::AF_UNSPEC):Validated arguments strictly, raising
IPAddr::InvalidPrefixErrorfor non-integer or out-of-range prefixes (0..32for IPv4,0..128for IPv6) andIPAddr::AddressFamilyErrorfor an unspecified or unsupported family.Added documentation (
README.md, RDoc) and tests covering all valid prefixes, invalid inputs, and existingIPAddr#netmaskbehavior.Design changes after review
The initial implementation in this PR was revised in response to review feedback:
IPAddr.ipv4_subnet_masksandIPAddr.ipv6_subnet_maskshelpers, then replaced them with a singleIPAddr.netmask(prefix, family)method to match the existingIPAddr#netmaskinstance method.IPAddr.new(0, family).mask(prefix).netmaskafter argument validation.IPAddr#netmask?: Initially implemented the optionalnetmask?method from Enhancement: add constants for IPv4 and IPv6 subnet masks #83, then dropped it because an address alone does not indicate whether it is used as a netmask.Memory measurement log
For comparison with the constant-table proposal in #83, the following local run measures the size of precomputed tables vs. a single returned string (
ObjectSpace.memsize_of):