Created
May 9, 2019 12:54
-
-
Save cristianrasch/a74e5a66fe31b95d50f8c9351f20e3fd 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
module ArrayUtils | |
def self.flatten(array) | |
array.inject([]) { |accu, elem| | |
if elem.kind_of?(Array) | |
accu.concat(flatten(elem)) | |
else | |
accu << elem | |
end | |
} | |
end | |
end | |
if __FILE__ == $0 | |
require 'minitest/autorun' | |
class ArrayUtilsTest < Minitest::Test | |
def test_it_works | |
assert_equal [1, 2, 3, 4], ArrayUtils.flatten([[1,2,[3]],4]) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment