mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
improve Enumerable#reverse_each to be efficient
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user