Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature deaggregate(IP) #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

martin-schmidt
Copy link

Takes IP and returns Array of Subnets.

   IP.new('1.2.0.0').deaggregate(IP.new('1.3.255.255'))
     => [#<IP::V4 1.2.0.0/15>]
   IP.new('1.2.0.0').deaggregate(IP.new('1.4.255.255'))
     => [#<IP::V4 1.2.0.0/15>, #<IP::V4 1.4.0.0/16>]
   IP.new('2001:db8:85a3:8d3::').deaggregate(IP.new('2001:0db8:85a3:08d3:ffff:ffff:ffff:ffff'))
     => [#<IP::V6 2001:db8:85a3:8d3::/64>]
   IP.new('2001:db8:85a3:8d3::').deaggregate(IP.new('2001:db8:85a3:8d3:1::'))
     => [#<IP::V6 2001:db8:85a3:8d3::/80>, #<IP::V6 2001:db8:85a3:8d3:1::>]

@candlerb
Copy link
Contributor

I had to think a bit about what this is trying to achieve.

What I believe it is doing is taking an inclusive range of IP addresses (ip1..ip2) and returning the smallest number of prefix-aligned subnets which covers that range. So I would argue what it is actually doing is aggregating, not de-aggregating.

There is an existing to_range function which is almost the inverse of this.

So how about a class method on IP which takes a range, and returns an array of IP objects which covers that range? I'm not sure exactly what that should be called though.

@martin-schmidt
Copy link
Author

What I believe it is doing is taking an inclusive range of IP addresses (ip1..ip2) and returning the smallest number of prefix-aligned subnets which covers that range. So I would argue what it is actually doing is aggregating, not de-aggregating.

There is an existing to_range function which is almost the inverse of this.

That's exactly what it's doing. It's called deaggregate in ipcalc, so I stuck with this to be consistent and because I haven't had a better idea.

It was the easiest way for me not to use ranges or a class method, because I use it with already initialized /32 objects, but ranges may be the proper way to do it in some other use cases.

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