Skip to content

Instantly share code, notes, and snippets.

@fugufish
Created October 13, 2017 16:52
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 fugufish/5b0d751d2491e732b78fc961f07da243 to your computer and use it in GitHub Desktop.
Save fugufish/5b0d751d2491e732b78fc961f07da243 to your computer and use it in GitHub Desktop.
require 'rspec'
def flatten(array)
ret = []
array.each do |i|
if i.is_a?(Array)
ret += flatten(i)
next
end
ret << i
end
ret
end
describe '#flatten' do
it 'should flatten a given array' do
expect(flatten([[1,2,[3]],4])).to eq([1,2,3,4])
end
it 'should flatten the array regardless of the level of nesting' do
expect(flatten([[1,2,[3,5,[6,7]]],4])).to eq([1,2,3,5,6,7,4])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment