class Array

def _keep(arr, cls, bool)

def _keep(arr, cls, bool)
  case cls
  when Class, Module
    arr.keep_if { |i| i.is_a?(cls) == bool }
  when Proc
    arr.keep_if { |i| !!cls.call(i) == bool }
  else
    arr.keep_if { |i| (i == cls) == bool }
  end
end

def join_and(separator: ', ', and_separator: ' and ', serial: true)

Returns:
  • (String) -

Parameters:
  • serial (Boolean) -- Use serial separators (e.g., serial commas)
  • and_separator (String) -- The separator for the last element
  • separator (String) -- The separator for all but the last element
def join_and(separator: ', ', and_separator: ' and ', serial: true)
  if length < 3
    join(and_separator)
  else
    start = self[0..-2]
    start.join(separator) + "#{serial ? separator.strip : ''}#{and_separator}#{last}"
  end
end

def join_or(separator: ', ', or_separator: ' or ', serial: true)

Returns:
  • (String) -

Other tags:
    See: Array#join_and -
def join_or(separator: ', ', or_separator: ' or ', serial: true)
  join_and(separator: separator, and_separator: or_separator, serial: serial)
end

def pop_sample


Pop a random element from the array.
def pop_sample
  delete_at(rand(length))
end

def that_are(*cls)

Returns:
  • (Array) -
def that_are(*cls)
  result = dup
  cls.each do |c|
    _keep result, c, true
  end
  result
end

def that_are_not(*cls)

Returns:
  • (Array) -
def that_are_not(*cls)
  result = dup
  cls.each do |c|
    _keep result, c, false
  end
  result
end