mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/ipaddr.rb (IPAddr#{eql?,hash}): Add IPAddr#{eql?,hash} so
that an IPAddr object can be used as a hash key, a set element, etc.; suggested by Nick Brown <nick@nick-brown.com>. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24388 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
60ab77a16a
commit
61c11677c8
2 changed files with 41 additions and 0 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
Tue Aug 4 15:06:58 2009 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
|
* lib/ipaddr.rb (IPAddr#{eql?,hash}): Add IPAddr#{eql?,hash} so
|
||||||
|
that an IPAddr object can be used as a hash key, a set element,
|
||||||
|
etc.; suggested by Nick Brown <nick@nick-brown.com>.
|
||||||
|
|
||||||
Tue Aug 4 13:07:10 2009 NARUSE, Yui <naruse@ruby-lang.org>
|
Tue Aug 4 13:07:10 2009 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
* include/ruby/oniguruma.h (ONIG_SYN_WARN_CC_DUP): defined.
|
* include/ruby/oniguruma.h (ONIG_SYN_WARN_CC_DUP): defined.
|
||||||
|
|
|
@ -331,6 +331,16 @@ class IPAddr
|
||||||
end
|
end
|
||||||
include Comparable
|
include Comparable
|
||||||
|
|
||||||
|
# Checks equality used by Hash.
|
||||||
|
def eql?(other)
|
||||||
|
return self.class == other.class && self.hash == other.hash && self == other
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a hash value used by Hash, Set, and Array classes
|
||||||
|
def hash
|
||||||
|
return (@addr.hash << 1) | (ipv4? ? 0 : 1)
|
||||||
|
end
|
||||||
|
|
||||||
# Creates a Range object for the network address.
|
# Creates a Range object for the network address.
|
||||||
def to_range
|
def to_range
|
||||||
begin_addr = (@addr & @mask_addr)
|
begin_addr = (@addr & @mask_addr)
|
||||||
|
@ -810,4 +820,29 @@ class TC_Operator < Test::Unit::TestCase
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_hash
|
||||||
|
a1 = IPAddr.new('192.168.2.0')
|
||||||
|
a2 = IPAddr.new('192.168.2.0')
|
||||||
|
a3 = IPAddr.new('3ffe:505:2::1')
|
||||||
|
a4 = IPAddr.new('3ffe:505:2::1')
|
||||||
|
a5 = IPAddr.new('127.0.0.1')
|
||||||
|
a6 = IPAddr.new('::1')
|
||||||
|
|
||||||
|
h = { a1 => 'ipv4', a2 => 'ipv4', a3 => 'ipv6', a4 => 'ipv6', a5 => 'ipv4', a6 => 'ipv6' }
|
||||||
|
assert_equal(4, 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)
|
||||||
|
assert_equal(true, s.include?(a1))
|
||||||
|
assert_equal(true, s.include?(a2))
|
||||||
|
assert_equal(true, s.include?(a3))
|
||||||
|
assert_equal(true, s.include?(a4))
|
||||||
|
assert_equal(true, s.include?(a5))
|
||||||
|
assert_equal(true, s.include?(a6))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue