Skip to content

Instantly share code, notes, and snippets.

@bbuchalter
Created February 22, 2018 17:02
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 bbuchalter/811778460f94c7ffae9c194c6dff286c to your computer and use it in GitHub Desktop.
Save bbuchalter/811778460f94c7ffae9c194c6dff286c to your computer and use it in GitHub Desktop.
require 'minitest/autorun'
class TestMyFlatten < Minitest::Test
def test_deeply_nested_arrays
assert_equal [1,2,3,4], MyFlatten.new([[1,2,[3]],4]).flatten
end
def test_argument_error
assert_raises ArgumentError do
MyFlatten.new("not an array")
end
end
end
class MyFlatten
def initialize(original)
raise ArgumentError unless original.is_a?(Array)
@original = original
end
def flatten
flattened = []
original.each do |element|
if element.is_a?(Array)
flattened += self.class.new(element).flatten
else
flattened << element
end
end
flattened
end
private
attr_reader :original
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment