summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-array-ext
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-03-24 18:39:06 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-03-24 18:39:06 +0900
commitc326ab877e8e9d04b672d14b37c7d5fa96d04e7b (patch)
treeb30c9487e551457f1a311d8672820bf7069b7469 /mrbgems/mruby-array-ext
parent23610d548956cc2ba92fada791ac1de1b1026192 (diff)
downloadmruby-c326ab877e8e9d04b672d14b37c7d5fa96d04e7b.tar.gz
mruby-c326ab877e8e9d04b672d14b37c7d5fa96d04e7b.zip
improve Enumerable#reverse_each to be efficient
Diffstat (limited to 'mrbgems/mruby-array-ext')
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb10
1 files changed, 10 insertions, 0 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb
index 337cef632..feec10ead 100644
--- a/mrbgems/mruby-array-ext/mrblib/array.rb
+++ b/mrbgems/mruby-array-ext/mrblib/array.rb
@@ -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