Skip to content

Instantly share code, notes, and snippets.

@cristianrasch
Created May 9, 2019 12:54
Show Gist options
  • Save cristianrasch/a74e5a66fe31b95d50f8c9351f20e3fd to your computer and use it in GitHub Desktop.
Save cristianrasch/a74e5a66fe31b95d50f8c9351f20e3fd to your computer and use it in GitHub Desktop.
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