Skip to content

Commit

Permalink
Added IPv6#network method, thanks to Leif Gensert (closes #12)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluemonk committed May 10, 2011
1 parent c95e3e6 commit dba60ba
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
== ipaddress 0.8.0

NEW:: IPv6#network


== ipaddress 0.7.5

CHANGED:: IPAddress::IPv4#each_host to improve speed
Expand Down
4 changes: 2 additions & 2 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -924,8 +924,8 @@ Feel free to join us and tell us what you think!
Thanks to Luca Russo (vargolo) and Simone Carletti
(weppos) for all the support and technical review. Thanks to Marco Beri,
Bryan T. Richardson, Nicolas Fevrier, jdpace, Daniele Alessandri, jrdioko,
Ghislain Charrier, Pawel Krzesniak, Mark Sullivan, Erik Ahlström and
Steve Rawlinson for their support, feedback and bug reports.
Ghislain Charrier, Pawel Krzesniak, Mark Sullivan, Leif Gensert,
Erik Ahlström and Steve Rawlinson for their support, feedback and bug reports.

== Copyright

Expand Down
13 changes: 13 additions & 0 deletions lib/ipaddress/ipv6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,19 @@ def literal
@address.gsub(":","-") + ".ipv6-literal.net"
end

#
# Returns a new IPv6 object with the network number
# for the given IP.
#
# ip = IPAddress "2001:db8:1:1:1:1:1:1/32"
#
# ip.network.to_string
# #=> "2001:db8::/32"
#
def network
self.class.parse_u128(network_u128, @prefix)
end

#
# Extract 16 bits groups from a string
#
Expand Down
13 changes: 13 additions & 0 deletions test/ipaddress/ipv6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def setup

@invalid_ipv6 = [":1:2:3:4:5:6:7",
":1:2:3:4:5:6:7"]

@networks = {
"2001:db8:1:1:1:1:1:1/32" => "2001:db8::/32",
"2001:db8:1:1:1:1:1::/32" => "2001:db8::/32",
"2001:db8::1/64" => "2001:db8::/64"}

@ip = @klass.new "2001:db8::8:800:200c:417a/64"
@network = @klass.new "2001:db8:8:800::/64"
Expand Down Expand Up @@ -186,6 +191,14 @@ def test_method_loopback?
assert_equal true, @klass.new("::1").loopback?
assert_equal false, @ip.loopback?
end

def test_method_network
@networks.each do |addr,net|
ip = @klass.new addr
assert_instance_of @klass, ip.network
assert_equal net, ip.network.to_string
end
end

def test_classmethod_expand
compressed = "2001:db8:0:cd30::"
Expand Down

0 comments on commit dba60ba

Please sign in to comment.