diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-04-26 01:53:15 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-04-26 01:53:15 +0900 |
| commit | 2202e412ea6ac44a5bcdaa2640fda0b7485437ca (patch) | |
| tree | 3e217007696283ad64188ae979a1c0de33a0d947 /mrbgems/mruby-array-ext/mrblib/array.rb | |
| parent | 44ec41a7724ef3591e9d4033655fa12e79b9ff2b (diff) | |
| parent | b8e5cb71fe4b7e5377facf23ecebd0dd4bc4d1ab (diff) | |
| download | mruby-2202e412ea6ac44a5bcdaa2640fda0b7485437ca.tar.gz mruby-2202e412ea6ac44a5bcdaa2640fda0b7485437ca.zip | |
Merge branch 'master' of github.com:mruby/mruby
Diffstat (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb')
| -rw-r--r-- | mrbgems/mruby-array-ext/mrblib/array.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 67f7e51a7..9ef7eb166 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -627,4 +627,34 @@ class Array end self end + + ## + # call-seq: + # ary.keep_if { |item| block } -> ary + # ary.keep_if -> Enumerator + # + # Deletes every element of +self+ for which the given block evaluates to + # +false+. + # + # See also Array#select! + # + # If no block is given, an Enumerator is returned instead. + # + # a = [1, 2, 3, 4, 5] + # a.keep_if { |val| val > 3 } #=> [4, 5] + + def keep_if(&block) + return to_enum :keep_if unless block_given? + + idx = 0 + len = self.size + while idx < self.size do + if block.call(self[idx]) + idx += 1 + else + self.delete_at(idx) + end + end + self + end end |
