ipaddr
ipaddr copied to clipboard
Allow reading unmasked address when passed a CIDR string
This behavior caught me by surprise:
> ip = IPAddr.new('1.2.3.4/16')
=> #<IPAddr: IPv4:1.2.0.0/255.255.0.0>
> ip.to_s
=> "1.2.0.0"
Looking at the code I believe this is happening at https://github.com/ruby/ipaddr/blob/master/lib/ipaddr.rb#L649-L651, which is a very old commit, so this behavior is clearly expected. For my use case I need the host bits unmasked, so I want to parse 1.2.3.4/16 and be able to read back something like ip.address as 1.2.3.4 and the netmask as 255.255.0.0. It looks like that's not currently possible with this gem, which surprises me. Am I overlooking a method, or is there a nuance in the CIDR spec implemented here I've forgotten?