class Array

Public Instance Methods

after(value) click to toggle source
# File lib/lite/ruby/array.rb, line 55
def after(value)
  return unless include?(value)

  self[(index(value) + 1) % size]
end
all_after(value) click to toggle source
# File lib/lite/ruby/array.rb, line 61
def all_after(value)
  return unless include?(value)

  i = index(value)
  return if i == (size - 1)

  self[(i + 1)..-1]
end
all_before(value) click to toggle source
# File lib/lite/ruby/array.rb, line 70
def all_before(value)
  return unless include?(value)

  i = index(value)
  return if i.zero?

  self[0..(i - 1)]
end
assert_all_min_values!(*valid_values) click to toggle source
# File lib/lite/ruby/array.rb, line 19
def assert_all_min_values!(*valid_values)
  return assert_min_values!(*valid_values) unless empty?

  raise ArgumentError, 'An empty array is not allowed'
end
assert_all_valid_values!(*valid_values) click to toggle source
# File lib/lite/ruby/array.rb, line 35
def assert_all_valid_values!(*valid_values)
  return assert_valid_values!(*valid_values) unless empty?

  raise ArgumentError, 'An empty array is not allowed'
end
assert_all_value_presence!() click to toggle source
# File lib/lite/ruby/array.rb, line 49
def assert_all_value_presence!
  return assert_value_presence! unless empty?

  raise ArgumentError, 'An empty array is not allowed'
end
assert_min_values!(*valid_values) click to toggle source
# File lib/lite/ruby/array.rb, line 5
def assert_min_values!(*valid_values)
  return self if empty?

  valid_values.each do |value|
    next if include?(value)

    raise ArgumentError,
          "Missing value: #{value.inspect}. " \
          "Minimum values are: #{valid_values.map(&:inspect).join(', ')}"
  end

  self
end
assert_valid_values!(*valid_values) click to toggle source
# File lib/lite/ruby/array.rb, line 25
def assert_valid_values!(*valid_values)
  each do |value|
    next if valid_values.include?(value)

    raise ArgumentError,
          "Invalid value: #{value.inspect}. " \
          "Allowed values are: #{valid_values.map(&:inspect).join(', ')}"
  end
end
assert_value_presence!() click to toggle source
# File lib/lite/ruby/array.rb, line 41
def assert_value_presence!
  each do |value|
    next if value.respond_to?(:present?) ? value.present? : value

    raise ArgumentError, "A #{value.inspect} value is not allowed"
  end
end
before(value) click to toggle source
# File lib/lite/ruby/array.rb, line 79
def before(value)
  return unless include?(value)

  self[(index(value) - 1) % size]
end
bury(*args) click to toggle source

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity rubocop:disable Style/GuardClause, Style/IfInsideElse

# File lib/lite/ruby/array.rb, line 87
def bury(*args)
  if args.count < 2
    raise ArgumentError, '2 or more arguments required'
  elsif args.count == 2
    if args[0].is_a?(Integer)
      self[args[0]] = args[1]
    else
      self << { args[0] => args[1] }
    end
  else
    if args[0].is_a?(Integer)
      arg = args.shift
      self[arg] = [] unless self[arg]
      self[arg].bury(*args)
    else
      self << {}.bury(*args)
    end
  end

  self
end
compact_blank()
Alias for: strip
compact_blank!()
Alias for: strip!
contains_all?(other) click to toggle source

rubocop:enable Style/GuardClause, Style/IfInsideElse rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity

# File lib/lite/ruby/array.rb, line 111
def contains_all?(other)
  (other & self) == self
end
contains_any?(other) click to toggle source
# File lib/lite/ruby/array.rb, line 115
def contains_any?(other)
  !contains_none?(other)
end
contains_none?(other) click to toggle source
# File lib/lite/ruby/array.rb, line 119
def contains_none?(other)
  (other & self).empty?
end
deep_dup() click to toggle source
# File lib/lite/ruby/safe/array.rb, line 5
def deep_dup
  map(&:deep_dup)
end
delete_first() click to toggle source
# File lib/lite/ruby/array.rb, line 123
def delete_first
  self[1..-1]
end
delete_first!() click to toggle source
# File lib/lite/ruby/array.rb, line 127
def delete_first!
  replace(delete_first)
end
delete_last() click to toggle source
# File lib/lite/ruby/array.rb, line 131
def delete_last
  self[0...-1]
end
delete_last!() click to toggle source
# File lib/lite/ruby/array.rb, line 135
def delete_last!
  replace(delete_last)
end
delete_values(*args) click to toggle source
# File lib/lite/ruby/array.rb, line 139
def delete_values(*args)
  args.each_with_object([]) { |val, array| array << delete(val) }
end
demote(value) click to toggle source
# File lib/lite/ruby/array.rb, line 143
def demote(value)
  sort_by { |val| val == value ? 0 : -1 }
end
demote!(value) click to toggle source
# File lib/lite/ruby/array.rb, line 147
def demote!(value)
  replace(demote(value))
end
denillify(identity = 0) click to toggle source
# File lib/lite/ruby/array.rb, line 151
def denillify(identity = 0)
  map { |val| val || identity }
end
denillify!(identity = 0) click to toggle source
# File lib/lite/ruby/array.rb, line 155
def denillify!(identity = 0)
  replace(denillify(identity))
end
divergence(other) click to toggle source
# File lib/lite/ruby/array.rb, line 159
def divergence(other)
  (self - other) | (other - self)
end
duplicates(minimum = 2) click to toggle source
# File lib/lite/ruby/array.rb, line 163
def duplicates(minimum = 2)
  hash = Hash.new(0)
  each { |val| hash[val] += 1 }
  hash.delete_if { |_, val| val < minimum }.keys
end
except(*values) click to toggle source
# File lib/lite/ruby/array.rb, line 169
def except(*values)
  reject { |val| values.include?(val) }
end
Also aliased as: reject_values
except!(*values) click to toggle source
# File lib/lite/ruby/array.rb, line 173
def except!(*values)
  reject! { |val| values.include?(val) }
  self
end
Also aliased as: reject_values!
extract!() { |element| ... } click to toggle source
# File lib/lite/ruby/safe/array.rb, line 9
def extract!
  return to_enum(:extract!) { size } unless block_given?

  extracted_elements = []
  reject! { |element| extracted_elements << element if yield(element) }
  extracted_elements
end
from(position) click to toggle source
# File lib/lite/ruby/safe/array.rb, line 17
def from(position)
  self[position, size] || []
end
fulfill(value, amount) click to toggle source
# File lib/lite/ruby/array.rb, line 178
def fulfill(value, amount)
  return self if amount <= size

  fill(value, size..(amount - 1))
end
groups(number) click to toggle source
# File lib/lite/ruby/array.rb, line 184
def groups(number)
  return [] if number <= 0

  num, rem = size.divmod(number)
  collection = (0..(num - 1)).collect { |val| self[(val * number), number] }
  return collection unless rem.positive?

  collection << self[-rem, rem]
end
in_groups(number, fill_with = nil) { || ... } click to toggle source

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength

# File lib/lite/ruby/safe/array.rb, line 22
def in_groups(number, fill_with = nil, &block)
  collection_size = size
  division = collection_size.div(number)
  modulo = collection_size % number
  collection = []
  start = 0

  number.times do |int|
    mod_gt_zero = modulo.positive?
    grouping = division + (mod_gt_zero && modulo > int ? 1 : 0)
    collection << last_group = slice(start, grouping)
    last_group << fill_with if (fill_with != false) && mod_gt_zero && (grouping == division)
    start += grouping
  end

  return collection unless defined?(yield)

  collection.each(&block)
end
in_groups_of(number, fill_with = nil) { || ... } click to toggle source

rubocop:disable Metrics/MethodLength, Style/GuardClause

# File lib/lite/ruby/safe/array.rb, line 44
def in_groups_of(number, fill_with = nil, &block)
  if number.to_i <= 0
    raise ArgumentError, "Group size must be a positive integer, was #{number.inspect}"
  elsif fill_with == false
    collection = self
  else
    padding = (number - size % number) % number
    collection = dup.concat(Array.new(padding, fill_with))
  end

  sliced_collection = collection.each_slice(number)
  return sliced_collection.to_a unless defined?(yield)

  sliced_collection(&block)
end
indexes(value) click to toggle source
# File lib/lite/ruby/array.rb, line 194
def indexes(value)
  array = []
  each_with_index { |val, i| array << i if value == val }
  array
end
Also aliased as: indices
indices(value)
Alias for: indexes
match(value) click to toggle source
# File lib/lite/ruby/array.rb, line 200
def match(value)
  find { |val| value == val }
end
merge(*values) click to toggle source
# File lib/lite/ruby/array.rb, line 204
def merge(*values)
  dup.merge!(*values)
end
merge!(*values) click to toggle source
# File lib/lite/ruby/array.rb, line 208
def merge!(*values)
  values.each_with_object(self) { |val, arr| arr.concat(val) }
end
nillify() click to toggle source
# File lib/lite/ruby/array.rb, line 212
def nillify
  map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
end
nillify!() click to toggle source
# File lib/lite/ruby/array.rb, line 216
def nillify!
  replace(nillify)
end
only(*values) click to toggle source
# File lib/lite/ruby/array.rb, line 220
def only(*values)
  select { |val| values.include?(val) }
end
Also aliased as: select_values
only!(*values) click to toggle source
# File lib/lite/ruby/array.rb, line 224
def only!(*values)
  select! { |val| values.include?(val) }
  self
end
Also aliased as: select_values!
position(value) click to toggle source
# File lib/lite/ruby/array.rb, line 229
def position(value)
  idx = index(value)
  return idx if idx.nil?

  idx + 1
end
positions(value) click to toggle source
# File lib/lite/ruby/array.rb, line 236
def positions(value)
  indexes(value).map { |val| val + 1 }
end
probability() click to toggle source
# File lib/lite/ruby/array.rb, line 240
def probability
  hash = Hash.new(0.0)
  differ = 0.0

  each do |val|
    hash[val] += 1.0
    differ += 1.0
  end

  hash.each_key { |val| hash[val] /= differ }
  hash
end
promote(value) click to toggle source
# File lib/lite/ruby/array.rb, line 253
def promote(value)
  sort_by { |val| val == value ? -1 : 0 }
end
promote!(value) click to toggle source
# File lib/lite/ruby/array.rb, line 257
def promote!(value)
  sort_by! { |val| val == value ? -1 : 0 }
end
rand_sample(max = nil) click to toggle source
# File lib/lite/ruby/array.rb, line 261
def rand_sample(max = nil)
  amount = rand(1..(max || size))
  sample(amount)
end
reject_values(*values)
Alias for: except
reject_values!(*values)
Alias for: except!
rposition(value) click to toggle source
# File lib/lite/ruby/array.rb, line 266
def rposition(value)
  idx = rindex(value)
  return idx if idx.nil?

  idx + 1
end
sample!() click to toggle source
# File lib/lite/ruby/array.rb, line 273
def sample!
  delete_at(Random.rand(size - 1))
end
select_values(*values)
Alias for: only
select_values!(*values)
Alias for: only!
split(value = nil) click to toggle source

rubocop:disable Metrics/MethodLength, Style/ExplicitBlockArgument

# File lib/lite/ruby/safe/array.rb, line 62
def split(value = nil)
  arr = dup
  result = []

  if block_given?
    while (idx = arr.index { |i| yield(i) })
      result << arr.shift(idx)
      arr.shift
    end
  else
    while (idx = arr.index(value))
      result << arr.shift(idx)
      arr.shift
    end
  end

  result << arr
end
strip() click to toggle source
# File lib/lite/ruby/array.rb, line 277
def strip
  reject(&:blank?)
end
Also aliased as: compact_blank
strip!() click to toggle source
# File lib/lite/ruby/array.rb, line 281
def strip!
  reject!(&:blank?)
  self
end
Also aliased as: compact_blank!
swap(from, to) click to toggle source
# File lib/lite/ruby/array.rb, line 286
def swap(from, to)
  self[from], self[to] = self[to], self[from]
  self
end
to(position) click to toggle source

rubocop:enable Metrics/MethodLength, Style/ExplicitBlockArgument

# File lib/lite/ruby/safe/array.rb, line 82
def to(position)
  return first(position + 1) if position >= 0

  self[0..position]
end
to_sentence(options = {}) click to toggle source
# File lib/lite/ruby/safe/array.rb, line 88
def to_sentence(options = {})
  words_connector = options[:words_connector] || ', '
  two_words_connector = options[:two_words_connector] || ' and '
  last_word_connector = options[:last_word_connector] || ', and '

  case size
  when 0 then ''
  when 1 then self[0].to_s
  when 2 then "#{self[0]}#{two_words_connector}#{self[1]}"
  else "#{self[0...-1].join(words_connector)}#{last_word_connector}#{self[-1]}"
  end
end