Skip to content

Instantly share code, notes, and snippets.

@sandric
Created December 1, 2017 05:50
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 sandric/aa1675b9d72fa3cd4bcabf7800a4eed5 to your computer and use it in GitHub Desktop.
Save sandric/aa1675b9d72fa3cd4bcabf7800a4eed5 to your computer and use it in GitHub Desktop.
Custom flatten function for Citrusbyte application
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