Skip to content

Instantly share code, notes, and snippets.

@g-ilham
Last active January 23, 2017 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g-ilham/805e44041a000579c69906a51a7d5f21 to your computer and use it in GitHub Desktop.
Save g-ilham/805e44041a000579c69906a51a7d5f21 to your computer and use it in GitHub Desktop.
class FlatArray
  attr_reader :nested_array, :flatten_array

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

  def call
    flat(nested_array)
    flatten_array
  end

  private

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