Skip to content

Commit

Permalink
added #to range and fixed ipaddress-gem#40
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemackintosh committed Mar 23, 2015
1 parent 09bde2a commit 6bf73cf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/ipaddress/ipv4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,26 @@ def reverse
end
alias_method :arpa, :reverse

#
# Return a list of IP's between @address
# and the supplied IP
#
# ip = IPAddress("172.16.100.51/32")
#
# ip.to("172.16.100.100")
# #=> ["172.16.100.51",
# "172.16.100.52",
# ...
# "172.16.100.99",
# "172.16.100.100"]
#
def to(e)
unless e.is_a? IPAddress::IPv4
e = IPv4.new(e)
end

Range.new(@u32, e.to_u32).map{|i| IPAddress.ntoa(i) }
end
#
# Splits a network into different subnets
#
Expand Down Expand Up @@ -1028,7 +1048,7 @@ def self.parse_classful(ip)

# Tweaked to remove the #upto(32)
def newprefix(num)
return @prefix + (Math::log2(num).to_i)
return @prefix + (Math::log2(num).ceil )
end

def sum_first_found(arr)
Expand Down
13 changes: 13 additions & 0 deletions test/ipaddress/ipv4_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def setup
"10.1.1.1" => 8,
"150.1.1.1" => 16,
"200.1.1.1" => 24 }

@in_range = {
"10.32.0.1" => ["10.32.0.253", 253],
"192.0.0.0" => ["192.1.255.255", 131072]
}

end

Expand Down Expand Up @@ -551,6 +556,14 @@ def test_network_split
assert_equal x.split(256).length, 256
end
end

def test_in_range
@in_range.each do |s,d|
ip = @klass.new(s)
assert_equal ip.to(d[0]).length, d[1]
end
end

end # class IPv4Test


0 comments on commit 6bf73cf

Please sign in to comment.