improve Enumerable#reverse_each to be efficient

parent 23610d54
......@@ -201,4 +201,14 @@ class Array
self.replace(result)
end
end
# for efficiency
def reverse_each(&block)
i = self.size - 1
while i>=0
block.call(self[i])
i -= 1
end
self
end
end
......@@ -519,8 +519,8 @@ module Enumerable
def reverse_each(&block)
ary = []
self.each {|*val| ary.unshift(*val) }
ary.each {|*val| block.call(*val) }
self.each {|*val| ary.push(val.__svalue) }
ary.reverse_each(&block)
self
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