summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorbluemonk <ceresa@gmail.com>2011-05-14 01:23:29 +0200
committerbluemonk <ceresa@gmail.com>2011-05-14 01:23:29 +0200
commitc2945c56542c6c4f969782b8e50e039cee9665ca (patch)
treeaa81848768ab701296df01e7118f0b54a29d5544 /lib
parent9120fad3ccee359f07ab0f12f964f7b5a8069676 (diff)
downloadipaddress-c2945c56542c6c4f969782b8e50e039cee9665ca.tar.gz
Added IPv6#<=>, changed IPv4#<=> and IPv4#supernet (closes #13)
Diffstat (limited to 'lib')
-rw-r--r--lib/ipaddress/ipv4.rb47
-rw-r--r--lib/ipaddress/ipv6.rb37
2 files changed, 62 insertions, 22 deletions
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb
index e3f9ab7..76ece23 100644
--- a/lib/ipaddress/ipv4.rb
+++ b/lib/ipaddress/ipv4.rb
@@ -426,14 +426,22 @@ module IPAddress;
end
#
- # Spaceship operator to compare IP addresses
+ # Spaceship operator to compare IPv4 objects
#
- # An IP address is considered to be minor if it
- # has a greater prefix (thus smaller hosts
- # portion) and a smaller u32 value.
+ # Comparing IPv4 addresses is useful to ordinate
+ # them into lists that match our intuitive
+ # perception of ordered IP addresses.
+ #
+ # The first comparison criteria is the u32 value.
+ # For example, 10.100.100.1 will be considered
+ # to be less than 172.16.0.1, because, in a ordered list,
+ # we expect 10.100.100.1 to come before 172.16.0.1.
#
- # For example, "10.100.100.1/8" is smaller than
- # "172.16.0.1/16", but it's bigger than "10.100.100.1/16".
+ # The second criteria, in case two IPv4 objects
+ # have identical addresses, is the prefix. An higher
+ # prefix will be considered greater than a lower
+ # prefix. This is because we expect to see
+ # 10.100.100.0/24 come before 10.100.100.0/25.
#
# Example:
#
@@ -443,22 +451,15 @@ module IPAddress;
#
# ip1 < ip2
# #=> true
- # ip1 < ip3
+ # ip1 > ip3
# #=> false
#
+ # [ip1,ip2,ip3].sort.map{|i| i.to_string}
+ # #=> ["10.100.100.1/8","10.100.100.1/16","172.16.0.1/16"]
+ #
def <=>(oth)
- if to_u32 > oth.to_u32
- return 1
- elsif to_u32 < oth.to_u32
- return -1
- else
- if prefix < oth.prefix
- return 1
- elsif prefix > oth.prefix
- return -1
- end
- end
- return 0
+ return prefix <=> oth.prefix if to_u32 == oth.to_u32
+ to_u32 <=> oth.to_u32
end
#
@@ -643,11 +644,13 @@ module IPAddress;
#
# ip.supernet(22).to_string
# #=> "172.16.8.0/22"
- #
+ #
+ # If +new_prefix+ is less than 1, returns 0.0.0.0/0
+ #
def supernet(new_prefix)
- raise ArgumentError, "Can't supernet a /1 network" if new_prefix < 1
raise ArgumentError, "New prefix must be smaller than existing prefix" if new_prefix >= @prefix.to_i
- self.class.new(@address+"/#{new_prefix}").network
+ return self.class.new("0.0.0.0/0") if new_prefix < 1
+ return self.class.new(@address+"/#{new_prefix}").network
end
#
diff --git a/lib/ipaddress/ipv6.rb b/lib/ipaddress/ipv6.rb
index eb4b13c..2532003 100644
--- a/lib/ipaddress/ipv6.rb
+++ b/lib/ipaddress/ipv6.rb
@@ -446,6 +446,43 @@ module IPAddress;
end
#
+ # Spaceship operator to compare IPv6 objects
+ #
+ # Comparing IPv6 addresses is useful to ordinate
+ # them into lists that match our intuitive
+ # perception of ordered IP addresses.
+ #
+ # The first comparison criteria is the u128 value.
+ # For example, 2001:db8:1::1 will be considered
+ # to be less than 2001:db8:2::1, because, in a ordered list,
+ # we expect 2001:db8:1::1 to come before 2001:db8:2::1.
+ #
+ # The second criteria, in case two IPv6 objects
+ # have identical addresses, is the prefix. An higher
+ # prefix will be considered greater than a lower
+ # prefix. This is because we expect to see
+ # 2001:db8:1::1/64 come before 2001:db8:1::1/65
+ #
+ # Example:
+ #
+ # ip1 = IPAddress "2001:db8:1::1/64"
+ # ip2 = IPAddress "2001:db8:2::1/64"
+ # ip3 = IPAddress "2001:db8:1::1/65"
+ #
+ # ip1 < ip2
+ # #=> true
+ # ip1 < ip3
+ # #=> false
+ #
+ # [ip1,ip2,ip3].sort.map{|i| i.to_string}
+ # #=> ["2001:db8:1::1/64","2001:db8:1::1/65","2001:db8:2::1/64"]
+ #
+ def <=>(oth)
+ return prefix <=> oth.prefix if to_u128 == oth.to_u128
+ to_u128 <=> oth.to_u128
+ end
+
+ #
# Returns the address portion of an IP in binary format,
# as a string containing a sequence of 0 and 1
#