From c7cff199dc14a7fb7a43910f885ca7fa9b2ccdfd Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Thu, 16 Jan 2014 21:33:48 +0900 Subject: hash value of enumerable should be obtained from its elements; close #1658 --- mrblib/enum.rb | 9 +++++++++ mrblib/range.rb | 9 +++++++++ test/t/array.rb | 4 +--- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/mrblib/enum.rb b/mrblib/enum.rb index 0593e1f04..e6aa682dd 100644 --- a/mrblib/enum.rb +++ b/mrblib/enum.rb @@ -395,4 +395,13 @@ module Enumerable # # ISO 15.3.2.2.20 alias to_a entries + + # redefine #hash 15.3.1.3.15 + def hash + h = 12347 + self.each do |e| + h ^= e.hash + end + h + end end diff --git a/mrblib/range.rb b/mrblib/range.rb index d43e3c223..7ea795078 100644 --- a/mrblib/range.rb +++ b/mrblib/range.rb @@ -28,6 +28,15 @@ class Range end self end + + # redefine #hash 15.3.1.3.15 + def hash + h = first.hash ^ last.hash + if self.exclude_end? + h += 1 + end + h + end end ## diff --git a/test/t/array.rb b/test/t/array.rb index 8ef1eee3f..5e745354f 100644 --- a/test/t/array.rb +++ b/test/t/array.rb @@ -301,6 +301,7 @@ assert('Array#hash', '15.2.12.5.35') do a = [ 1, 2, 3 ] assert_true(a.hash.is_a? Integer) + assert_equal([1,2].hash, [1,2].hash) end assert('Array#<=>', '15.2.12.5.36') do @@ -328,6 +329,3 @@ assert("Array (Longish inline array)") do ary.each {|p| h[p.class] += 1} assert_equal({Array=>200}, h) end - - - -- cgit v1.2.3