Skip to content

Instantly share code, notes, and snippets.

@IlkhamGaysin
Last active August 23, 2017 16:15
Show Gist options
  • Save IlkhamGaysin/283e0e83bae29efc3488de2ba74035a5 to your computer and use it in GitHub Desktop.
Save IlkhamGaysin/283e0e83bae29efc3488de2ba74035a5 to your computer and use it in GitHub Desktop.
class FlatArray
  attr_reader :nested_array, :result

  def initialize(nested_array = [])
    @nested_array = nested_array
    @result = []
  end

  def call
    flat(nested_array)
    
    result
  end

  private

  def flat(nested_array)
    nested_array.each do |element|
      element.is_a?(Array) ? flat(element) : @result << element
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment