Skip to content

Commit 181f7b9

Browse files
committed
Fix some Hash methods are inconsistent with values
Inconsistent when hash has duplicate key. ### Example ```ruby # example.rb keys = (1..3).map{[_1]} h = keys.to_h{[_1, _1[0]]} keys[0][0] = 2 p h.values p h.each_value.to_a p h ``` #### Before this patch: ```console $ bin/mruby example.rb [1, 2, 3] [1, 1, 3] {[2]=>1, [2]=>1, [3]=>3} ``` #### After this patch (same as Ruby) ```console $ bin/mruby example.rb [1, 2, 3] [1, 2, 3] {[2]=>1, [2]=>2, [3]=>3} ```
1 parent 3491372 commit 181f7b9

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

mrblib/hash.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def each_key(&block)
140140
def each_value(&block)
141141
return to_enum :each_value unless block
142142

143-
self.keys.each{|k| block.call(self[k])}
143+
self.values.each{|v| block.call(v)}
144144
self
145145
end
146146

@@ -192,11 +192,11 @@ def _inspect(recur_list)
192192
recur_list[self.object_id] = true
193193
ary=[]
194194
keys=self.keys
195+
vals=self.values
195196
size=keys.size
196197
i=0
197198
while i<size
198-
k=keys[i]
199-
ary<<(k._inspect(recur_list) + "=>" + self[k]._inspect(recur_list))
199+
ary<<(keys[i]._inspect(recur_list) + "=>" + vals[i]._inspect(recur_list))
200200
i+=1
201201
end
202202
"{"+ary.join(", ")+"}"

0 commit comments

Comments
 (0)