Skip to content

Instantly share code, notes, and snippets.

@diego-aslz
Created June 7, 2019 00:19
Show Gist options
  • Save diego-aslz/7dfb3619cf6057911779b3aebc5a6240 to your computer and use it in GitHub Desktop.
Save diego-aslz/7dfb3619cf6057911779b3aebc5a6240 to your computer and use it in GitHub Desktop.
def flatten(arr, into: [])
arr.each do |el|
case el
when Array then flatten(el, into: into)
else into.push(el)
end
end
into
end
require 'rspec'
require_relative 'flatten'
describe 'flatten' do
it 'flattens arrays' do
expect(flatten([[1, 2, [3]], 4])).to eq([1, 2, 3, 4])
expect(flatten([[1], [[2]]])).to eq([1, 2])
end
end
source 'https://rubygems.org'
gem 'rspec'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment