# Class: Insertion

Inherits:
  Object
  - Object
  - Insertion
  show all

Defined in:
  lib/yard/core_ext/insertion.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 collapse

- #after(val, recursive = false) ⇒ Object

  Inserts the value after `val`.

- #after_any(val) ⇒ Object

  Alias for #after with `recursive` set to true.

- #before(val, recursive = false) ⇒ Object

  Inserts the value before `val`.

- #before_any(val) ⇒ Object

  Alias for #before with `recursive` set to true.

- #initialize(list, value) ⇒ Insertion constructor

  Creates an insertion object on a list with a value to be inserted.

## Constructor Details

### #initialize(list, value) ⇒ `Insertion`

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:

- list (`Array`) —

  the list to perform the insertion on

- value (`Object`) —

  the value to insert

    14
    15
    16
    17

    # File 'lib/yard/core_ext/insertion.rb', line 14

    def initialize(list, value)
      @list = list
      @values = (Array === value ? value : [value])
    end

## Instance Method Details

### #after(val, recursive = false) ⇒ `Object`

Inserts the value after `val`.

#### Examples:

#####

If subsections are ignored

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

Parameters:

- val (`Object`) —

  the object the value will be inserted after

- recursive (`Boolean`) (defaults to: `false`) —

  look inside sublists

    30

    # File 'lib/yard/core_ext/insertion.rb', line 30

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

### #after_any(val) ⇒ `Object`

Alias for #after with `recursive` set to true

Since:

- 0.6.0

    38

    # File 'lib/yard/core_ext/insertion.rb', line 38

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

### #before(val, recursive = false) ⇒ `Object`

Inserts the value before `val`

Parameters:

- val (`Object`) —

  the object the value will be inserted before

- recursive (`Boolean`) (defaults to: `false`) —

  look inside sublists

    22

    # File 'lib/yard/core_ext/insertion.rb', line 22

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

### #before_any(val) ⇒ `Object`

Alias for #before with `recursive` set to true

Since:

- 0.6.0

    34

    # File 'lib/yard/core_ext/insertion.rb', line 34

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