diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-04-28 05:53:52 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-04-28 05:53:52 +0900 |
| commit | b0fd5229fca77f6ec0e39dfe5e87778ad09188d4 (patch) | |
| tree | 63d5091964af0aed47e898be948f89dc107344ea /mrbgems/mruby-array-ext/mrblib/array.rb | |
| parent | e1f4e021dea13102496b5c8a669a8a2e670f12f1 (diff) | |
| parent | cdef46e3d99835c48592ab08d1383612786820a7 (diff) | |
| download | mruby-b0fd5229fca77f6ec0e39dfe5e87778ad09188d4.tar.gz mruby-b0fd5229fca77f6ec0e39dfe5e87778ad09188d4.zip | |
Merge pull request #2139 from suzukaze/add-array.select_bang
Add Array#select_bang
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 9ef7eb166..bddcd8ab8 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -657,4 +657,34 @@ class Array end self end + + ## + # call-seq: + # ary.select! {|item| block } -> ary or nil + # ary.select! -> Enumerator + # + # Invokes the given block passing in successive elements from +self+, + # deleting elements for which the block returns a +false+ value. + # + # If changes were made, it will return +self+, otherwise it returns +nil+. + # + # See also Array#keep_if + # + # If no block is given, an Enumerator is returned instead. + + def select!(&block) + return to_enum :select! 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 + return nil if self.size == len + self + end end |
