Class: Insertion

Inherits:
Object
  • Object
show all
Defined in:
lib/yard/core_ext/array.rb

Overview

The Insertion class inserts a value before or after another value in a list.

Examples:

  Insertion.new([1, 2, 3], 4).before(3) # => [1, 2, 4, 3]

Instance Method Summary

Constructor Details

- (Insertion) initialize(list, value)

Creates an insertion object on a list with a value to be inserted. To finalize the insertion, call #before or #after on the object.

Parameters:

  • (Array) list — the list to perform the insertion on
  • (Object) value — the value to insert


29
# File 'lib/yard/core_ext/array.rb', line 29

def initialize(list, value) @list, @value = list, value end

Instance Method Details

- (Object) after(val, ignore_subsections = true)

Inserts the value after val.

Examples:

If subsections are ignored

  Insertion.new([1, [2], 3], :X).after(1) # => [1, [2], :X, 3]

Parameters:

  • (Object) val — the object the value will be inserted after
  • (Boolean) ignore_subsections (defaults to: true) — treat any Array objects that follow val as associated and do not split them up.


42
# File 'lib/yard/core_ext/array.rb', line 42

def after(val, ignore_subsections = true) insertion(val, 1, ignore_subsections) end

- (Object) before(val)

Inserts the value before val

Parameters:

  • (Object) val — the object the value will be inserted before


33
# File 'lib/yard/core_ext/array.rb', line 33

def before(val) insertion(val, 0) end