diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2015-09-10 23:03:53 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2015-09-10 23:03:53 +0900 |
| commit | 698b3c925d757cd7113f9916db2dbb7e2eabf2f4 (patch) | |
| tree | 65f39e481766d5333657de62c2453b7612153831 /mrblib/hash.rb | |
| parent | 543ac88eae2afdf25364720b3b8cac9ce0748fad (diff) | |
| download | mruby-698b3c925d757cd7113f9916db2dbb7e2eabf2f4.tar.gz mruby-698b3c925d757cd7113f9916db2dbb7e2eabf2f4.zip | |
add Hash#rehash to handle key modification; ref #2945
Diffstat (limited to 'mrblib/hash.rb')
| -rw-r--r-- | mrblib/hash.rb | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/mrblib/hash.rb b/mrblib/hash.rb index cb52c1ffe..48ac96e56 100644 --- a/mrblib/hash.rb +++ b/mrblib/hash.rb @@ -319,6 +319,29 @@ class Hash h end + ## + # call-seq: + # hsh.rehash -> hsh + # + # Rebuilds the hash based on the current hash values for each key. If + # values of key objects have changed since they were inserted, this + # method will reindex <i>hsh</i>. + # + # h = {"AAA" => "b"} + # h.keys[0].chop! + # h #=> {"AA"=>"b"} + # h["AA"] #=> nil + # h.rehash #=> {"AA"=>"b"} + # h["AA"] #=> "b" + # + def rehash + h = {} + self.each{|k,v| + h[k] = v + } + self.replace(h) + end + def __update(h) h.each_key{|k| self[k] = h[k]} self |
