module Refinements::Array

Provides additional enhancements to the Array primitive.

Public Instance Methods

combinatorial?(other) click to toggle source
# File lib/refinements/array.rb, line 11
def combinatorial?(other) = !other.empty? && size == union(other).size

def compress = compact.delete_if { |element| element.respond_to?(:empty?) && element.empty? }

def compress!
  delete_if { |element| element.respond_to?(:empty?) && element.empty? }
  compact!
end

def excluding(*elements) = self - elements.flatten

def filter_find(&)
  warn "`#{self.class}##{__method__}` is deprecated and will be removed in Version 13.0.0.",
       category: :deprecated

  block_given? ? lazy.map(&).find(&:itself) : lazy
end

def including(*elements) = self + elements.flatten

def intersperse(*elements) = product([elements]).tap(&:pop).flatten.push(last)

def maximum(key) = map(&key).max

def mean = size.zero? ? 0 : sum(0.0) / size

def minimum(key) = map(&key).min

def pad(value, max = size) = dup.fill(value, size..(max - 1))

def pick(*keys)
  return if empty?

  keys.many? ? keys.map { |key| first[key] } : first[keys.first]
end

def pluck(*keys)
  return dup if empty?
  return [] if keys.empty?

  if keys.many?
    map { |element| keys.map { |key| element[key] } }
  else
    key = keys.first
    map { |element| element[key] }
  end
end

def replace_at(index, *elements)
  delete_at index
  insert(index, *elements)
end

def ring(&) = [last, *self, first].each_cons(3, &)

def supplant target, *replacements
  index(target).then do |position|
    delete_at position
    insert position, *replacements
  end

  self
end

def supplant_if target, *replacements
  each { |item| supplant target, *replacements if item == target }
  self
end

def to_sentence conjunction = "and", delimiter: ", "
  case length
    when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
    when 2 then join " #{conjunction} "
    else join
  end
end

def to_usage conjunction = "and", delimiter: ", "
  map(&:inspect).to_sentence conjunction
compress(= compact.delete_if { |element| element.respond_to?(:empty?) && element.empty? }) click to toggle source
# File lib/refinements/array.rb, line 13
def compress = compact.delete_if { |element| element.respond_to?(:empty?) && element.empty? }

def compress!
  delete_if { |element| element.respond_to?(:empty?) && element.empty? }
  compact!
end

def excluding(*elements) = self - elements.flatten

def filter_find(&)
  warn "`#{self.class}##{__method__}` is deprecated and will be removed in Version 13.0.0.",
       category: :deprecated

  block_given? ? lazy.map(&).find(&:itself) : lazy
end

def including(*elements) = self + elements.flatten

def intersperse(*elements) = product([elements]).tap(&:pop).flatten.push(last)

def maximum(key) = map(&key).max

def mean = size.zero? ? 0 : sum(0.0) / size

def minimum(key) = map(&key).min

def pad(value, max = size) = dup.fill(value, size..(max - 1))

def pick(*keys)
  return if empty?

  keys.many? ? keys.map { |key| first[key] } : first[keys.first]
end

def pluck(*keys)
  return dup if empty?
  return [] if keys.empty?

  if keys.many?
    map { |element| keys.map { |key| element[key] } }
  else
    key = keys.first
    map { |element| element[key] }
  end
end

def replace_at(index, *elements)
  delete_at index
  insert(index, *elements)
end

def ring(&) = [last, *self, first].each_cons(3, &)

def supplant target, *replacements
  index(target).then do |position|
    delete_at position
    insert position, *replacements
  end

  self
end

def supplant_if target, *replacements
  each { |item| supplant target, *replacements if item == target }
  self
end

def to_sentence conjunction = "and", delimiter: ", "
  case length
    when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
    when 2 then join " #{conjunction} "
    else join
  end
end

def to_usage conjunction = "and", delimiter: ", "
  map(&:inspect).to_sentence conjunction, 
compress!() click to toggle source
# File lib/refinements/array.rb, line 15
def compress!
  delete_if { |element| element.respond_to?(:empty?) && element.empty? }
  compact!
end
excluding(*elements) click to toggle source
# File lib/refinements/array.rb, line 20
def excluding(*elements) = self - elements.flatten

def filter_find(&)
  warn "`#{self.class}##{__method__}` is deprecated and will be removed in Version 13.0.0.",
       category: :deprecated

  block_given? ? lazy.map(&).find(&:itself) : lazy
end

def including(*elements) = self + elements.flatten

def intersperse(*elements) = product([elements]).tap(&:pop).flatten.push(last)

def maximum(key) = map(&key).max

def mean = size.zero? ? 0 : sum(0.0) / size

def minimum(key) = map(&key).min

def pad(value, max = size) = dup.fill(value, size..(max - 1))

def pick(*keys)
  return if empty?

  keys.many? ? keys.map { |key| first[key] } : first[keys.first]
end

def pluck(*keys)
  return dup if empty?
  return [] if keys.empty?

  if keys.many?
    map { |element| keys.map { |key| element[key] } }
  else
    key = keys.first
    map { |element| element[key] }
  end
end

def replace_at(index, *elements)
  delete_at index
  insert(index, *elements)
end

def ring(&) = [last, *self, first].each_cons(3, &)

def supplant target, *replacements
  index(target).then do |position|
    delete_at position
    insert position, *replacements
  end

  self
end

def supplant_if target, *replacements
  each { |item| supplant target, *replacements if item == target }
  self
end

def to_sentence conjunction = "and", delimiter: ", "
  case length
    when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
    when 2 then join " #{conjunction} "
    else join
  end
end

def to_usage conjunction = "and", delimiter: ", "
  map(&:inspect).to_sentence conjunction, delimiter:
filter_find(&) click to toggle source
# File lib/refinements/array.rb, line 22
def filter_find(&)
  warn "`#{self.class}##{__method__}` is deprecated and will be removed in Version 13.0.0.",
       category: :deprecated

  block_given? ? lazy.map(&).find(&:itself) : lazy
end
including(*elements) click to toggle source
# File lib/refinements/array.rb, line 29
def including(*elements) = self + elements.flatten

def intersperse(*elements) = product([elements]).tap(&:pop).flatten.push(last)

def maximum(key) = map(&key).max

def mean = size.zero? ? 0 : sum(0.0) / size

def minimum(key) = map(&key).min

def pad(value, max = size) = dup.fill(value, size..(max - 1))

def pick(*keys)
  return if empty?

  keys.many? ? keys.map { |key| first[key] } : first[keys.first]
end

def pluck(*keys)
  return dup if empty?
  return [] if keys.empty?

  if keys.many?
    map { |element| keys.map { |key| element[key] } }
  else
    key = keys.first
    map { |element| element[key] }
  end
end

def replace_at(index, *elements)
  delete_at index
  insert(index, *elements)
end

def ring(&) = [last, *self, first].each_cons(3, &)

def supplant target, *replacements
  index(target).then do |position|
    delete_at position
    insert position, *replacements
  end

  self
end

def supplant_if target, *replacements
  each { |item| supplant target, *replacements if item == target }
  self
end

def to_sentence conjunction = "and", delimiter: ", "
  case length
    when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
    when 2 then join " #{conjunction} "
    else join
  end
end

def to_usage conjunction = "and", delimiter: ", "
  map(&:inspect).to_sentence conjunction, delimiter:
end
intersperse(*elements) click to toggle source
# File lib/refinements/array.rb, line 31
def intersperse(*elements) = product([elements]).tap(&:pop).flatten.push(last)

def maximum(key) = map(&key).max

def mean = size.zero? ? 0 : sum(0.0) / size

def minimum(key) = map(&key).min

def pad(value, max = size) = dup.fill(value, size..(max - 1))

def pick(*keys)
  return if empty?

  keys.many? ? keys.map { |key| first[key] } : first[keys.first]
end

def pluck(*keys)
  return dup if empty?
  return [] if keys.empty?

  if keys.many?
    map { |element| keys.map { |key| element[key] } }
  else
    key = keys.first
    map { |element| element[key] }
  end
end

def replace_at(index, *elements)
  delete_at index
  insert(index, *elements)
end

def ring(&) = [last, *self, first].each_cons(3, &)

def supplant target, *replacements
  index(target).then do |position|
    delete_at position
    insert position, *replacements
  end

  self
end

def supplant_if target, *replacements
  each { |item| supplant target, *replacements if item == target }
  self
end

def to_sentence conjunction = "and", delimiter: ", "
  case length
    when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
    when 2 then join " #{conjunction} "
    else join
  end
end

def to_usage conjunction = "and", delimiter: ", "
  map(&:inspect).to_sentence conjunction, delimiter:
end
    
maximum(key) click to toggle source
# File lib/refinements/array.rb, line 33
  def maximum(key) = map(&key).max

  def mean = size.zero? ? 0 : sum(0.0) / size

  def minimum(key) = map(&key).min

  def pad(value, max = size) = dup.fill(value, size..(max - 1))

  def pick(*keys)
    return if empty?

    keys.many? ? keys.map { |key| first[key] } : first[keys.first]
  end

  def pluck(*keys)
    return dup if empty?
    return [] if keys.empty?

    if keys.many?
      map { |element| keys.map { |key| element[key] } }
    else
      key = keys.first
      map { |element| element[key] }
    end
  end

  def replace_at(index, *elements)
    delete_at index
    insert(index, *elements)
  end

  def ring(&) = [last, *self, first].each_cons(3, &)

  def supplant target, *replacements
    index(target).then do |position|
      delete_at position
      insert position, *replacements
    end

    self
  end

  def supplant_if target, *replacements
    each { |item| supplant target, *replacements if item == target }
    self
  end

  def to_sentence conjunction = "and", delimiter: ", "
    case length
      when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
      when 2 then join " #{conjunction} "
      else join
    end
  end

  def to_usage conjunction = "and", delimiter: ", "
    map(&:inspect).to_sentence conjunction, delimiter:
  end
end
mean(= size.zero? ? 0 : sum(0.0) / size) click to toggle source
# File lib/refinements/array.rb, line 35
    def mean = size.zero? ? 0 : sum(0.0) / size

    def minimum(key) = map(&key).min

    def pad(value, max = size) = dup.fill(value, size..(max - 1))

    def pick(*keys)
      return if empty?

      keys.many? ? keys.map { |key| first[key] } : first[keys.first]
    end

    def pluck(*keys)
      return dup if empty?
      return [] if keys.empty?

      if keys.many?
        map { |element| keys.map { |key| element[key] } }
      else
        key = keys.first
        map { |element| element[key] }
      end
    end

    def replace_at(index, *elements)
      delete_at index
      insert(index, *elements)
    end

    def ring(&) = [last, *self, first].each_cons(3, &)

    def supplant target, *replacements
      index(target).then do |position|
        delete_at position
        insert position, *replacements
      end

      self
    end

    def supplant_if target, *replacements
      each { |item| supplant target, *replacements if item == target }
      self
    end

    def to_sentence conjunction = "and", delimiter: ", "
      case length
        when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
        when 2 then join " #{conjunction} "
        else join
      end
    end

    def to_usage conjunction = "and", delimiter: ", "
      map(&:inspect).to_sentence conjunction, delimiter:
    end
  end
end
minimum(key) click to toggle source
# File lib/refinements/array.rb, line 37
      def minimum(key) = map(&key).min

      def pad(value, max = size) = dup.fill(value, size..(max - 1))

      def pick(*keys)
        return if empty?

        keys.many? ? keys.map { |key| first[key] } : first[keys.first]
      end

      def pluck(*keys)
        return dup if empty?
        return [] if keys.empty?

        if keys.many?
          map { |element| keys.map { |key| element[key] } }
        else
          key = keys.first
          map { |element| element[key] }
        end
      end

      def replace_at(index, *elements)
        delete_at index
        insert(index, *elements)
      end

      def ring(&) = [last, *self, first].each_cons(3, &)

      def supplant target, *replacements
        index(target).then do |position|
          delete_at position
          insert position, *replacements
        end

        self
      end

      def supplant_if target, *replacements
        each { |item| supplant target, *replacements if item == target }
        self
      end

      def to_sentence conjunction = "and", delimiter: ", "
        case length
          when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
          when 2 then join " #{conjunction} "
          else join
        end
      end

      def to_usage conjunction = "and", delimiter: ", "
        map(&:inspect).to_sentence conjunction, delimiter:
      end
    end
  end
end
pad(value, max = size) click to toggle source
# File lib/refinements/array.rb, line 39
    def pad(value, max = size) = dup.fill(value, size..(max - 1))

    def pick(*keys)
      return if empty?

      keys.many? ? keys.map { |key| first[key] } : first[keys.first]
    end

    def pluck(*keys)
      return dup if empty?
      return [] if keys.empty?

      if keys.many?
        map { |element| keys.map { |key| element[key] } }
      else
        key = keys.first
        map { |element| element[key] }
      end
    end

    def replace_at(index, *elements)
      delete_at index
      insert(index, *elements)
    end

    def ring(&) = [last, *self, first].each_cons(3, &)

    def supplant target, *replacements
      index(target).then do |position|
        delete_at position
        insert position, *replacements
      end

      self
    end

    def supplant_if target, *replacements
      each { |item| supplant target, *replacements if item == target }
      self
    end

    def to_sentence conjunction = "and", delimiter: ", "
      case length
        when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
        when 2 then join " #{conjunction} "
        else join
      end
    end

    def to_usage conjunction = "and", delimiter: ", "
      map(&:inspect).to_sentence conjunction, delimiter:
    end
  end
end
pick(*keys) click to toggle source
# File lib/refinements/array.rb, line 41
def pick(*keys)
  return if empty?

  keys.many? ? keys.map { |key| first[key] } : first[keys.first]
end
pluck(*keys) click to toggle source
# File lib/refinements/array.rb, line 47
def pluck(*keys)
  return dup if empty?
  return [] if keys.empty?

  if keys.many?
    map { |element| keys.map { |key| element[key] } }
  else
    key = keys.first
    map { |element| element[key] }
  end
end
replace_at(index, *elements) click to toggle source
# File lib/refinements/array.rb, line 59
def replace_at(index, *elements)
  delete_at index
  insert(index, *elements)
end
ring(&) click to toggle source
# File lib/refinements/array.rb, line 64
  def ring(&) = [last, *self, first].each_cons(3, &)

  def supplant target, *replacements
    index(target).then do |position|
      delete_at position
      insert position, *replacements
    end

    self
  end

  def supplant_if target, *replacements
    each { |item| supplant target, *replacements if item == target }
    self
  end

  def to_sentence conjunction = "and", delimiter: ", "
    case length
      when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
      when 2 then join " #{conjunction} "
      else join
    end
  end

  def to_usage conjunction = "and", delimiter: ", "
    map(&:inspect).to_sentence conjunction, delimiter:
  end
end
supplant(target, *replacements) click to toggle source
# File lib/refinements/array.rb, line 66
def supplant target, *replacements
  index(target).then do |position|
    delete_at position
    insert position, *replacements
  end

  self
end
supplant_if(target, *replacements) click to toggle source
# File lib/refinements/array.rb, line 75
def supplant_if target, *replacements
  each { |item| supplant target, *replacements if item == target }
  self
end
to_sentence(conjunction = "and", delimiter: ", ") click to toggle source
# File lib/refinements/array.rb, line 80
def to_sentence conjunction = "and", delimiter: ", "
  case length
    when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
    when 2 then join " #{conjunction} "
    else join
  end
end
to_usage(conjunction = "and", delimiter: ", ") click to toggle source
# File lib/refinements/array.rb, line 88
def to_usage conjunction = "and", delimiter: ", "
  map(&:inspect).to_sentence conjunction, delimiter:
end