Skip to content

Add IPAddr#netmask class method - #117

Open
PharaohKJ wants to merge 1 commit into
ruby:masterfrom
PharaohKJ:issue-83-subnet-mask-helpers
Open

PharaohKJ wants to merge 1 commit into
ruby:masterfrom
PharaohKJ:issue-83-subnet-mask-helpers

Conversation

@PharaohKJ

@PharaohKJ PharaohKJ commented Aug 5, 2026 •

Copy link
Copy Markdown

Summary

What changed

  • Added IPAddr.netmask(prefix, family = Socket::AF_UNSPEC):

    IPAddr.netmask(24, Socket::AF_INET)  # => "255.255.255.0"
    IPAddr.netmask(64, Socket::AF_INET6) # => "ffff:ffff:ffff:ffff:0000:0000:0000:0000"
  • Validated arguments strictly, raising IPAddr::InvalidPrefixError for non-integer or out-of-range prefixes (0..32 for IPv4, 0..128 for IPv6) and IPAddr::AddressFamilyError for an unspecified or unsupported family.

  • Added documentation (README.md, RDoc) and tests covering all valid prefixes, invalid inputs, and existing IPAddr#netmask behavior.

Design changes after review

The initial implementation in this PR was revised in response to review feedback:

  • Unified API: Initially implemented separate IPAddr.ipv4_subnet_masks and IPAddr.ipv6_subnet_masks helpers, then replaced them with a single IPAddr.netmask(prefix, family) method to match the existing IPAddr#netmask instance method.
  • Simplified implementation: Initially implemented custom bit operations and string formatting, then switched to reusing IPAddr.new(0, family).mask(prefix).netmask after argument validation.
  • Removed IPAddr#netmask?: Initially implemented the optional netmask? 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):

$ bundle exec ruby -Ilib -robjspace -e 'require "ipaddr"; ipv4_table = (0..32).map { |p| IPAddr.netmask(p, Socket::AF_INET).dup.freeze }.freeze; ipv6_table = (0..128).map { |p| IPAddr.netmask(p, Socket::AF_INET6).dup.freeze }.freeze; v4 = ObjectSpace.memsize_of(ipv4_table) + ipv4_table.sum { |s| ObjectSpace.memsize_of(s) }; v6 = ObjectSpace.memsize_of(ipv6_table) + ipv6_table.sum { |s| ObjectSpace.memsize_of(s) }; one_v4 = IPAddr.netmask(24, Socket::AF_INET); one_v6 = IPAddr.netmask(64, Socket::AF_INET6); puts "Ruby #{RUBY_VERSION}"; puts "fixed-table IPV4_SUBNET_MASKS size: #{v4} bytes"; puts "fixed-table IPV6_SUBNET_MASKS size: #{v6} bytes"; puts "fixed-table TOTAL: #{v4 + v6} bytes"; puts "single call IPAddr.netmask(24, Socket::AF_INET) string size: #{ObjectSpace.memsize_of(one_v4)} bytes"; puts "single call IPAddr.netmask(64, Socket::AF_INET6) string size: #{ObjectSpace.memsize_of(one_v6)} bytes"'
Ruby 4.0.10
fixed-table IPV4_SUBNET_MASKS size: 1624 bytes
fixed-table IPV6_SUBNET_MASKS size: 11392 bytes
fixed-table TOTAL: 13016 bytes
single call IPAddr.netmask(24, Socket::AF_INET) string size: 80 bytes
single call IPAddr.netmask(64, Socket::AF_INET6) string size: 160 bytes

Comment thread lib/ipaddr.rb Resolved
Comment thread lib/ipaddr.rb Resolved
Comment thread lib/ipaddr.rb Resolved
@PharaohKJ

Copy link
Copy Markdown
Author

@taketo1113 san
Thanks for the review. I've updated the implementation based on your feedback—please see the updated PR description for details.

@PharaohKJ
PharaohKJ requested a review from taketo1113 November 24, 2026 01:49
@PharaohKJ

Copy link
Copy Markdown
Author

Equivalents for #83 examples

With this change, the examples proposed in #83 can be written as follows:

  • Subnet mask lookup (instead of IPAddr::IPV4_SUBNET_MASKS[24] / IPAddr::IPV6_SUBNET_MASKS[64]):

    Call IPAddr.netmask directly:

    IPAddr.netmask(24, Socket::AF_INET)  # => "255.255.255.0"
    IPAddr.netmask(64, Socket::AF_INET6) # => "ffff:ffff:ffff:ffff:0000:0000:0000:0000"

    Or define lookup tables in user code if precomputed arrays are needed:

    IPV4_SUBNET_MASKS = (0..32).map { |n| IPAddr.netmask(n, Socket::AF_INET) }.freeze
    IPV6_SUBNET_MASKS = (0..128).map { |n| IPAddr.netmask(n, Socket::AF_INET6) }.freeze
    
    IPV4_SUBNET_MASKS[24] # => "255.255.255.0"
    IPV6_SUBNET_MASKS[64] # => "ffff:ffff:ffff:ffff:0000:0000:0000:0000"
  • Netmask bit-pattern check (equivalent to the proposed netmask?):

    Using IPAddr#to_bits (handles both IPv4 and IPv6 without building tables):

    def netmask?(ipaddr)
      ipaddr.to_bits.match?(/\A1*0*\z/)
    end
    
    netmask?(IPAddr.new("255.255.255.0"))         # => true
    netmask?(IPAddr.new("255.254.255.0"))         # => false
    netmask?(IPAddr.new("ffff:ffff:ffff:ffff::")) # => true

    Or checking against the tables above with IPAddr#to_string (to_string is used instead of to_s so IPv6 addresses match the full form returned by IPAddr.netmask):

    def netmask?(ipaddr)
      masks = ipaddr.ipv4? ? IPV4_SUBNET_MASKS : IPV6_SUBNET_MASKS
      masks.include?(ipaddr.to_string)
    end
    
    netmask?(IPAddr.new("255.255.255.0"))         # => true
    netmask?(IPAddr.new("255.254.255.0"))         # => false
    netmask?(IPAddr.new("ffff:ffff:ffff:ffff::")) # => true

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"
@taketo1113
taketo1113 force-pushed the issue-83-subnet-mask-helpers branch from 75c69b7 to 2b3d6d8 Compare November 27, 2026 14:27
@taketo1113 taketo1113 changed the title implement issue #83 subnet mask helpers and netmask? with docs/tests. Add IPAddr#netmask class method Nov 27, 2026

@taketo1113 taketo1113 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants