retrieve values in Hash#each to handle modified keys

parent 077c87ba
...@@ -86,7 +86,14 @@ class Hash ...@@ -86,7 +86,14 @@ class Hash
def each(&block) def each(&block)
return to_enum :each unless block_given? 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 self
end end
...@@ -203,8 +210,7 @@ class Hash ...@@ -203,8 +210,7 @@ class Hash
return to_enum :reject! unless block_given? return to_enum :reject! unless block_given?
keys = [] keys = []
self.each_key{|k| self.each{|k,v|
v = self[k]
if b.call([k, v]) if b.call([k, v])
keys.push(k) keys.push(k)
end end
...@@ -221,8 +227,7 @@ class Hash ...@@ -221,8 +227,7 @@ class Hash
return to_enum :reject unless block_given? return to_enum :reject unless block_given?
h = {} h = {}
self.each_key{|k| self.each{|k,v|
v = self[k]
unless b.call([k, v]) unless b.call([k, v])
h[k] = v h[k] = v
end end
...@@ -235,8 +240,7 @@ class Hash ...@@ -235,8 +240,7 @@ class Hash
return to_enum :select! unless block_given? return to_enum :select! unless block_given?
keys = [] keys = []
self.each_key{|k| self.each{|k,v|
v = self[k]
unless b.call([k, v]) unless b.call([k, v])
keys.push(k) keys.push(k)
end end
...@@ -253,8 +257,7 @@ class Hash ...@@ -253,8 +257,7 @@ class Hash
return to_enum :select unless block_given? return to_enum :select unless block_given?
h = {} h = {}
self.each_key{|k| self.each{|k,v|
v = self[k]
if b.call([k, v]) if b.call([k, v])
h[k] = v h[k] = v
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment