Created
December 1, 2017 05:50
-
-
Save sandric/aa1675b9d72fa3cd4bcabf7800a4eed5 to your computer and use it in GitHub Desktop.
Custom flatten function for Citrusbyte application
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' | |
class Array | |
def custom_flatten | |
self.reduce([]) do |result, entry| | |
entry.kind_of?(Array) ? result += entry.custom_flatten : result << entry | |
end | |
end | |
end | |
RSpec.describe Array do | |
it 'flattens [1, 3, 3]' do | |
expect([1, 3, 4].custom_flatten).to eq [1, 3, 4] | |
end | |
it 'flattens [1, [2], 3]' do | |
expect([1, [2], 3].custom_flatten).to eq [1, 2, 3] | |
end | |
it 'flattens [1, 2, [3, 4, [5]]], 6, [7]]' do | |
numbers = [1, 2, [3, 4, [5]], 6, [7]] | |
expect(numbers.custom_flatten).to eq [1, 2, 3, 4, 5, 6, 7] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment