Created
July 29, 2016 17:28
-
-
Save amuhle/265ce0135fa431baaa75e57ec2596088 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
class ArrayUtil | |
attr_reader :array | |
def initialize(array) | |
@array = array | |
end | |
def super_flatten | |
@result = [] | |
recursive_flat @array | |
@result | |
end | |
private | |
def recursive_flat array | |
array.each do |item| | |
if item.is_a? Array | |
recursive_flat item | |
else | |
@result << item | |
end | |
end | |
end | |
end | |
class TestSuperFlatten < Minitest::Test | |
def test_multiple_array | |
test_array = [[1,2,[3]],4] | |
result = ArrayUtil.new(test_array).super_flatten | |
assert_equal [1,2,3,4], result | |
end | |
def test_simple_array | |
test_array = [1,3,5,[6,7]] | |
result = ArrayUtil.new(test_array).super_flatten | |
assert_equal [1,3,5,6,7], result | |
end | |
def test_empty_array | |
test_array = [] | |
result = ArrayUtil.new(test_array).super_flatten | |
assert_equal [], result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment