class Array def delete_first self[1..-1] end def delete_last self[0...-1] end unless method_defined?(:from) def from(position) self[position, size] || [] end end def groups(number) return([]) if number <= 0 n, r = size.divmod(number) collection = (0..(n - 1)).collect { |i| self[(i * number), number] } r > 0 ? collection << self[-r, r] : collection end unless method_defined?(:in_groups) def in_groups(number, fill_with=nil) collection_size = size division = collection_size.div(number) modulo = collection_size % number collection = [] start = 0 number.times do |i| grouping = division + (modulo > 0 && modulo > i ? 1 : 0) collection << last_group = slice(start, grouping) last_group << fill_with if fill_with != false && modulo > 0 && grouping == division start += grouping end block_given? ? collection.each { |g| yield(g) } : collection end end unless method_defined?(:in_groups_of) def in_groups_of(number, fill_with=nil) if number.to_i <= 0 raise ArgumentError, "Group size must be a positive integer, was #{number.inspect}" end if fill_with == false collection = self else padding = (number - size % number) % number collection = dup.concat(Array.new(padding, fill_with)) end block_given? ? collection.each_slice(number) { |slice| yield(slice) } : collection.each_slice(number).to_a end end unless method_defined?(:split) def split(number=nil) if block_given? inject([[]]) do |results, element| yield(element) ? results << [] : results.last << element results end else results, arr = [[]], dup until arr.empty? if (idx = arr.index(number)) results.last.concat(arr.shift(idx)) arr.shift results << [] else results.last.concat(arr.shift(arr.size)) end end results end end end def strip reject { |v| v.blank? } end unless method_defined?(:to) def to(position) position >= 0 ? first(position + 1) : self[0..position] end end unless method_defined?(:to_sentence) def to_sentence(options={}) options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector) default_connectors = { words_connector: ', ', two_words_connector: ' and ', last_word_connector: ', and ' } options = default_connectors.merge!(options) case size when 0 '' when 1 self[0].to_s.dup when 2 "#{self[0]}#{options[:two_words_connector]}#{self[1]}" else "#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}" end end end end