Class: Arrayie::Tools

Inherits:
Object
  • Object
show all
Defined in:
lib/arrayie/tools.rb

Overview

This class contains tools for working with Array type

Instance Method Summary collapse

Instance Method Details

#flatten(input_array) ⇒ Array

Flattens a Array type

Parameters:

  • input_array (Array)

    An array

Returns:

  • (Array)

    flattened input array

Raises:

  • (ArgumentError)

    if the input array is not an Array type



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/arrayie/tools.rb', line 25

def flatten(input_array)
  unless input_array.is_a?(Array)
    raise ArgumentError, 'Accepted input should be an Array type.'
  end

  input_array.inject([]) do |output_array, item|
    output_array += item.is_a?(Array) ? flatten(item) : [item]

    output_array
  end
end