module Rdoba::Mixin::Split_byArray

Public Instance Methods

split_by() { |v| ... } click to toggle source

split_by method splits the self array by a condition, which is evaliated in a passed block, in two versions that are the return value. Usage:

require 'rdoba'

rdoba mixin: :split_by

(first, second) = [0,1,2,3].split_by { |value| value % 2 == 0 } first # => [0,2] second # => [1,3]

# File lib/rdoba/mixin.rb, line 252
def split_by &block
   idxs = []
   rejected = self.reject.with_index do |v, i|
      yield( v ) && ( idxs << i ) ; end
      [ self.values_at(*idxs), rejected ] ; end