Created
February 9, 2017 12:00
-
-
Save duduribeiro/7cf24dbea7c5ad286c59db084690db5a to your computer and use it in GitHub Desktop.
Flatten
This file contains hidden or 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
class Flatten | |
def run(params = []) | |
result = [] | |
params.each do |value| | |
if value.is_a?(Array) | |
result = result | run(value) | |
else | |
result = result << value | |
end | |
end | |
result | |
end | |
end |
This file contains hidden or 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 | |
let(:array) { [[1,2,[3]],4] } | |
it 'flattens the array' do | |
result = Flatten.new.run(array) | |
expect(result).to eq([1,2,3,4]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment