diff options
| author | skandhas <[email protected]> | 2013-03-19 14:07:23 +0800 |
|---|---|---|
| committer | skandhas <[email protected]> | 2013-03-19 14:07:23 +0800 |
| commit | 9262a9f40ce81c7a4d1d94d989581b6c93012dba (patch) | |
| tree | 84bf64d21afae2c206f54633d96819ae32fbd75e /mrbgems/mruby-enum-ext/mrblib/enum.rb | |
| parent | ab79941c513b9244a055f0e4338ba0bc78dce261 (diff) | |
| download | mruby-9262a9f40ce81c7a4d1d94d989581b6c93012dba.tar.gz mruby-9262a9f40ce81c7a4d1d94d989581b6c93012dba.zip | |
add Enumerable#drop_while
Diffstat (limited to 'mrbgems/mruby-enum-ext/mrblib/enum.rb')
| -rw-r--r-- | mrbgems/mruby-enum-ext/mrblib/enum.rb | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb index 9c77577ca..bae8f3865 100644 --- a/mrbgems/mruby-enum-ext/mrblib/enum.rb +++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb @@ -20,5 +20,25 @@ module Enumerable self.each {|e| n == 0 ? ary << e : n -= 1 } ary end + + ## + # call-seq: + # enum.drop_while {|arr| block } -> array + # + # Drops elements up to, but not including, the first element for + # which the block returns +nil+ or +false+ and returns an array + # containing the remaining elements. + # + # a = [1, 2, 3, 4, 5, 0] + # a.drop_while {|i| i < 3 } #=> [3, 4, 5, 0] + + def drop_while(&block) + ary, state = [], false + self.each do |e| + state = true if !state and !block.call(e) + ary << e if state + end + ary + end end |
