class Insertion

Insertion.new([1, 2, 3], 4).before(3) # => [1, 2, 4, 3]
@example
value in a list.
The Insertion class inserts a value before or after another
frozen_string_literal: true

def after(val, recursive = false) insertion(val, 1, recursive) end

Parameters:
  • recursive (Boolean) -- look inside sublists
  • val (Object) -- the object the value will be inserted after

Other tags:
    Example: If subsections are ignored -
def after(val, recursive = false) insertion(val, 1, recursive) end

def after_any(val) insertion(val, 1, true) end

Other tags:
    Since: - 0.6.0
def after_any(val) insertion(val, 1, true) end

def before(val, recursive = false) insertion(val, 0, recursive) end

Parameters:
  • recursive (Boolean) -- look inside sublists
  • val (Object) -- the object the value will be inserted before
def before(val, recursive = false) insertion(val, 0, recursive) end

def before_any(val) insertion(val, 0, true) end

Other tags:
    Since: - 0.6.0
def before_any(val) insertion(val, 0, true) end

def initialize(list, value)

Parameters:
  • value (Object) -- the value to insert
  • list (Array) -- the list to perform the insertion on
def initialize(list, value)
  @list = list
  @values = (Array === value ? value : [value])
end

def insertion(val, rel, recursive = false, list = @list)

Parameters:
  • list (Array) -- the list to place objects into
  • recursive (Boolean) -- look inside sublists
  • rel (Fixnum) -- the relative index (0 or 1) of where the object
  • val (Object) -- the value to insert
def insertion(val, rel, recursive = false, list = @list)
  if recursive
    list.each do |item|
      next unless item.is_a?(Array)
      tmp = item.dup
      insertion(val, rel, recursive, item)
      return(list) unless item == tmp
    end
  end
  index = list.index(val)
  list[index + rel, 0] = @values if index
  list
end