summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-array-ext/mrblib
diff options
context:
space:
mode:
authorJun Hiroe <[email protected]>2014-04-24 22:49:52 +0900
committerJun Hiroe <[email protected]>2014-04-24 23:04:46 +0900
commitd4bec4d6571132e759658bb8819d71d96b83d55c (patch)
treed7af0f752ab92b33b36fd47e945421e940410d3a /mrbgems/mruby-array-ext/mrblib
parent39c036ab037b13d62149f4736ae7808e411d0029 (diff)
downloadmruby-d4bec4d6571132e759658bb8819d71d96b83d55c.tar.gz
mruby-d4bec4d6571132e759658bb8819d71d96b83d55c.zip
Add Array#delete_if
Diffstat (limited to 'mrbgems/mruby-array-ext/mrblib')
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb32
1 files changed, 32 insertions, 0 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb
index 7da416cf8..bf9c09223 100644
--- a/mrbgems/mruby-array-ext/mrblib/array.rb
+++ b/mrbgems/mruby-array-ext/mrblib/array.rb
@@ -513,4 +513,36 @@ class Array
self[idx, 0] = args
self
end
+
+ ##
+ # call-seq:
+ # ary.delete_if { |item| block } -> ary
+ # ary.delete_if -> Enumerator
+ #
+ # Deletes every element of +self+ for which block evaluates to +true+.
+ #
+ # The array is changed instantly every time the block is called, not after
+ # the iteration is over.
+ #
+ # See also Array#reject!
+ #
+ # If no block is given, an Enumerator is returned instead.
+ #
+ # scores = [ 97, 42, 75 ]
+ # scores.delete_if {|score| score < 80 } #=> [97]
+
+ def delete_if(&block)
+ return to_enum :delete_if unless block_given?
+
+ idx = 0
+ len = self.size
+ while idx < self.size do
+ if block.call(self[idx])
+ self.delete_at(idx)
+ else
+ idx += 1
+ end
+ end
+ self
+ end
end