Skip to content

Commit

Permalink
* lib/ipaddr.rb (IPAddr#hash): Take account of netmask; submitted
Browse files Browse the repository at this point in the history
  by Nobuhiro IMAI in [ruby-dev:39011]



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24411 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
knu committed Aug 5, 2009
1 parent 8bc6c71 commit 20a6847
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Thu Aug 6 00:09:56 2009 Akinori MUSHA <[email protected]>

* lib/ipaddr.rb (IPAddr#hash): Take account of netmask; submitted
by Nobuhiro IMAI in [ruby-dev:39011]

Wed Aug 5 19:19:13 2009 Nobuyoshi Nakada <[email protected]>

* ruby.c (load_file_internal): assumes -x flag if no "ruby" is in
Expand Down
12 changes: 7 additions & 5 deletions lib/ipaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def eql?(other)

# Returns a hash value used by Hash, Set, and Array classes
def hash
return (@addr.hash << 1) | (ipv4? ? 0 : 1)
return ([@addr, @mask_addr].hash << 1) | (ipv4? ? 0 : 1)
end

# Creates a Range object for the network address.
Expand Down Expand Up @@ -827,17 +827,19 @@ def test_hash
a4 = IPAddr.new('3ffe:505:2::1')
a5 = IPAddr.new('127.0.0.1')
a6 = IPAddr.new('::1')
a7 = IPAddr.new('192.168.2.0/25')
a8 = IPAddr.new('192.168.2.0/25')

h = { a1 => 'ipv4', a2 => 'ipv4', a3 => 'ipv6', a4 => 'ipv6', a5 => 'ipv4', a6 => 'ipv6' }
assert_equal(4, h.size)
h = { a1 => 'ipv4', a2 => 'ipv4', a3 => 'ipv6', a4 => 'ipv6', a5 => 'ipv4', a6 => 'ipv6', a7 => 'ipv4', a8 => 'ipv4'}
assert_equal(5, h.size)
assert_equal('ipv4', h[a1])
assert_equal('ipv4', h[a2])
assert_equal('ipv6', h[a3])
assert_equal('ipv6', h[a4])

require 'set'
s = Set[a1, a2, a3, a4, a5, a6]
assert_equal(4, s.size)
s = Set[a1, a2, a3, a4, a5, a6, a7, a8]
assert_equal(5, s.size)
assert_equal(true, s.include?(a1))
assert_equal(true, s.include?(a2))
assert_equal(true, s.include?(a3))
Expand Down

0 comments on commit 20a6847

Please sign in to comment.