Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
Create an array dinamically by pushing element inside only when the size of array (if present) is lower of equal to depth. Inside the class "ArrayFunctions" you will find a method called "squish" which will receive two parameters: array, depth.
arr.each_with_object([]) do |element, flattened|
flattened.push *(element.is_a?(Array) && n != nil && element.size <= n ? squish(element, n) : element)
end
As you can see we will loop the array until we reach last element inside, and we will push elements on a temporary array only when the element has been "squished" by this i mean that the stop flag will be add elements that have no children (are not an array) or if they do add them only is depth allows it (element.size <= n).
##Source code Sample app and source code can be downloaded here
###Or keep reading to see this sample gist below
|-- Gemfile
|-- Gemfile.lock
|-- Guardfile
|-- Rakefile
|-- README.md
|-- example.rb
|-- arrayfunctions.rb
|-- arrayfunctions_unit_test.rb
###Use instructions
#just require your helper class
require './arrayfunctions'
#there is no need to create an instance
puts ArrayFunctions.squish([1])
###Run test instructions
ruby arrayfunctions_test.rb