Created
June 7, 2019 00:19
-
-
Save diego-aslz/7dfb3619cf6057911779b3aebc5a6240 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
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 |
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
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 |
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
source 'https://rubygems.org' | |
gem 'rspec' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment