diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-04-12 10:42:02 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-04-12 10:42:02 +0900 |
| commit | 3308177c4d1a44120a1166f36624aff033e929e4 (patch) | |
| tree | 1e35426ad5e238666d87405271012fa462c15d5d | |
| parent | 077c87bad6448bcfae6e90a64c78f26d43a7ea68 (diff) | |
| download | mruby-3308177c4d1a44120a1166f36624aff033e929e4.tar.gz mruby-3308177c4d1a44120a1166f36624aff033e929e4.zip | |
retrieve values in Hash#each to handle modified keys
| -rw-r--r-- | mrblib/hash.rb | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/mrblib/hash.rb b/mrblib/hash.rb index 9a8bd8a7b..87e517b97 100644 --- a/mrblib/hash.rb +++ b/mrblib/hash.rb @@ -86,7 +86,14 @@ class Hash def each(&block) return to_enum :each unless block_given? - self.keys.each { |k| block.call [k, self[k]] } + keys = self.keys + vals = self.values + len = self.size + i = 0 + while i < len + block.call [keys[i], vals[i]] + i += 1 + end self end @@ -203,8 +210,7 @@ class Hash return to_enum :reject! unless block_given? keys = [] - self.each_key{|k| - v = self[k] + self.each{|k,v| if b.call([k, v]) keys.push(k) end @@ -221,8 +227,7 @@ class Hash return to_enum :reject unless block_given? h = {} - self.each_key{|k| - v = self[k] + self.each{|k,v| unless b.call([k, v]) h[k] = v end @@ -235,8 +240,7 @@ class Hash return to_enum :select! unless block_given? keys = [] - self.each_key{|k| - v = self[k] + self.each{|k,v| unless b.call([k, v]) keys.push(k) end @@ -253,8 +257,7 @@ class Hash return to_enum :select unless block_given? h = {} - self.each_key{|k| - v = self[k] + self.each{|k,v| if b.call([k, v]) h[k] = v end |
