Project

General

Profile

Actions

Feature #1877

closed

IPAddr#hash should consider a mask_addr

Added by no6v (Nobuhiro IMAI) over 15 years ago. Updated over 13 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-dev:39011]

Description

=begin
いまいです。

r24388 ですが、ネットマスクが考慮されていないので、アドレスが同じでネッ
トマスクが違うオブジェクトも同じものだとみなされてしまいます。

a1 = IPAddr.new("192.168.2.0/24")
=> #<IPAddr: IPv4:192.168.2.0/255.255.255.0>
a2 = IPAddr.new("192.168.2.0/25")
=> #<IPAddr: IPv4:192.168.2.0/255.255.255.128>
{a1 => 'ipv4', a2 => 'ipv4'}.size
=> 1

これらは別の範囲を指すオブジェクトだと思うので、ハッシュのキーとしては
別のものであるとうれしいです。とりあえずパッチです。

Nobuhiro IMAI
Key fingerprint = F39E D552 545D 7C64 D690 F644 5A15 746C BD8E 7106

Index: lib/ipaddr.rb

--- lib/ipaddr.rb (revision 24388)
+++ lib/ipaddr.rb (working copy)
@@ -338,7 +338,7 @@

# 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.

@@ -827,17 +827,19 @@
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))
    =end
Actions #1

Updated by knu (Akinori MUSHA) over 15 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

=begin
Applied in changeset r24411.
=end

Actions

Also available in: Atom PDF

Like0
Like0