module Enumerable
Public Instance Methods
group_by_chunks() { |first| ... }
click to toggle source
!method group_by_chunks
(&block)
Build a hash which maps elements matching the chunk condition to an array of subsequent elements which don't match the chunk condition. @example [1, 10, 11, 12, 2, 20, 21, 3, 30, 31, 32].group_by_chunks { _1 < 10 } # => { 1 => [10, 11, 12], 2 => [20, 21], 3 => [30, 31, 32] } @note The first element must match the chunk condition. @yield [Object] object to analyze @yieldreturn [Boolean] chunk condition: begin a new chunk with this object as key if the condition returns true @return [Hash]
# File lib/core_ext/enumerable.rb 47 def group_by_chunks 48 fail(ArgumentError, "first element must match chunk condition") unless yield(first) 49 slice_when { yield(_2) }.map { [_1.first, _1[1..]] }.to_h 50 end
split(*args, &block)
click to toggle source
!method split(object=nil, &block)
Divides an enumerable into sub-enumerables based on a delimiter, returning an array of these sub-enumerables. @example [1, 2, 0, 3, 4].split { _1 == 0 } # => [[1, 2], [3, 4]] [1, 2, 0, 3, 4].split(0) # => [[1, 2], [3, 4]] [0, 0, 1, 0, 2].split(0) # => [[], [] [1], [2]] [1, 0, 0, 2, 3].split(0) # => [[1], [], [2], [3]] [1, 0, 2, 0, 0].split(0) # => [[1], [2]] @note While similar to +Array#split+ from ActiveSupport, this core extension works for all enumerables and therefore works fine with. Nokogiri. Also, it behaves more like +String#split+ by ignoring any trailing zero-length sub-enumerators. @param object [Object] element at which to split @yield [Object] element to analyze @yieldreturn [Boolean] whether to split at this element or not @return [Array]
# File lib/core_ext/enumerable.rb 23 def split(*args, &block) 24 [].tap do |array| 25 while index = slice((start ||= 0)...length).find_index(*args, &block) 26 array << slice(start...start+index) 27 start += index + 1 28 end 29 array << slice(start..-1) if start < length 30 end 31 end