improve Enumerable#reverse_each to be efficient

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