Skip to content
Permalink
Browse files
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}
  ```
  • Loading branch information
shuujii committed May 9, 2020
1 parent 3491372 commit 181f7b97d6b5ac76d64e5457f28916b92aada619
Showing 1 changed file with 3 additions and 3 deletions.
@@ -140,7 +140,7 @@ def each_key(&block)
def each_value(&block)
return to_enum :each_value unless block

self.keys.each{|k| block.call(self[k])}
self.values.each{|v| block.call(v)}
self
end

@@ -192,11 +192,11 @@ def _inspect(recur_list)
recur_list[self.object_id] = true
ary=[]
keys=self.keys
vals=self.values
size=keys.size
i=0
while i<size
k=keys[i]
ary<<(k._inspect(recur_list) + "=>" + self[k]._inspect(recur_list))
ary<<(keys[i]._inspect(recur_list) + "=>" + vals[i]._inspect(recur_list))
i+=1
end
"{"+ary.join(", ")+"}"

0 comments on commit 181f7b9

Please sign in to comment.