summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorbluemonk <ceresa@gmail.com>2011-05-12 00:38:17 +0200
committerbluemonk <ceresa@gmail.com>2011-05-12 00:38:17 +0200
commit9120fad3ccee359f07ab0f12f964f7b5a8069676 (patch)
tree1371387ee42baada166d7a5183a05b609455cb0d /lib
parent05d1cc71bfa12bfdfe1bf61afed1b0fc78449531 (diff)
downloadipaddress-9120fad3ccee359f07ab0f12f964f7b5a8069676.tar.gz
Removed extension methods and extension directory
Diffstat (limited to 'lib')
-rw-r--r--lib/ipaddress.rb15
-rw-r--r--lib/ipaddress/extensions/extensions.rb22
-rw-r--r--lib/ipaddress/ipv4.rb22
3 files changed, 29 insertions, 30 deletions
diff --git a/lib/ipaddress.rb b/lib/ipaddress.rb
index 3627782..a9894f4 100644
--- a/lib/ipaddress.rb
+++ b/lib/ipaddress.rb
@@ -14,8 +14,6 @@
require 'ipaddress/ipv4'
require 'ipaddress/ipv6'
-require 'ipaddress/extensions/extensions'
-
module IPAddress
@@ -185,4 +183,17 @@ def IPAddress(str)
IPAddress::parse str
end
+#
+# Compatibility with Ruby 1.8
+#
+if RUBY_VERSION =~ /1\.8/
+ class Hash # :nodoc:
+ alias :key :index
+ end
+ class Math # :nodoc:
+ def Math.log2(n)
+ log(n) / log(2)
+ end
+ end
+end
diff --git a/lib/ipaddress/extensions/extensions.rb b/lib/ipaddress/extensions/extensions.rb
deleted file mode 100644
index 4e0bc88..0000000
--- a/lib/ipaddress/extensions/extensions.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-class << Math # :nodoc:
- def log2(n); log(n) / log(2); end
-end
-
-if RUBY_VERSION =~ /1\.8/
- class Hash # :nodoc:
- alias :key :index
- end
-end
-
-class Integer # :nodoc:
- def power_of_2?
- Math::log2(self).to_i == Math::log2(self)
- end
-
- def closest_power_of_2(limit=32)
- self.upto(limit) do |i|
- return i if i.power_of_2?
- end
- end
-end
-
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb
index d895cff..e3f9ab7 100644
--- a/lib/ipaddress/ipv4.rb
+++ b/lib/ipaddress/ipv4.rb
@@ -904,9 +904,6 @@ module IPAddress;
# * Class B, from 128.0.0.0 to 191.255.255.255
# * Class C, D and E, from 192.0.0.0 to 255.255.255.254
#
- # Note that classes C, D and E will all have a default
- # prefix of /24 or 255.255.255.0
- #
# Example:
#
# ip = IPAddress::IPv4.parse_classful "10.0.0.1"
@@ -916,6 +913,9 @@ module IPAddress;
# ip.a?
# #=> true
#
+ # Note that classes C, D and E will all have a default
+ # prefix of /24 or 255.255.255.0
+ #
def self.parse_classful(ip)
if IPAddress.valid_ipv4?(ip)
address = ip.strip
@@ -930,14 +930,24 @@ module IPAddress;
# private methods
#
private
+
+ def power_of_2?(int)
+ Math::log2(int).to_i == Math::log2(int)
+ end
+
+ def closest_power_of_2(num, limit=32)
+ num.upto(limit) do |i|
+ return i if power_of_2?(i)
+ end
+ end
def calculate_subnets(subnets)
- po2 = subnets.closest_power_of_2
+ po2 = closest_power_of_2(subnets)
new_prefix = @prefix + Math::log2(po2).to_i
networks = Array.new
(0..po2-1).each do |i|
mul = i * (2**(32-new_prefix))
- networks << IPAddress::IPv4.parse_u32(network_u32+mul, new_prefix)
+ networks << self.class.parse_u32(network_u32+mul, new_prefix)
end
until networks.size == subnets
networks = sum_first_found(networks)
@@ -948,7 +958,7 @@ module IPAddress;
def sum_first_found(arr)
dup = arr.dup.reverse
dup.each_with_index do |obj,i|
- a = [IPAddress::IPv4.summarize(obj,dup[i+1])].flatten
+ a = [self.class.summarize(obj,dup[i+1])].flatten
if a.size == 1
dup[i..i+1] = a
return dup.reverse