I wrote these two methods using Ruby 2.3.1, the tests require the minitest gem to be installed on your system.
You can run the tests using the command
ruby flatten_recursion_test.rb ruby flatten_regex_test.rb
Here is a summary of the two methods:
[].flatten_with_recursion
- I googled the question and found a solution that used recursion, though if the array is deep enough it will throw a SystemStackError error.
[].flatten_with_regex
- I thought it would be fun to use gsub to solve the problem. This approach does not throw a SystemStackError error when the array is very deep.