class Array
def press
each_with_object([]) do |elem, flat|
flat.push(*(elem.respond_to?(:each_with_object) ? elem.press : elem))
end
end
end
### Specs
require 'spec_helper'
RSpec.describe Array do
context '.press' do
it 'flattens a nested array of integers' do
nested = [[1, 2, [3]], 4]
expect(nested.press).to eq([1, 2, 3, 4])
end
it 'flattens a nested array of arbitrary things' do
object = Object.new
nested = [[1, 2, [object]], '4']
expect(nested.press).to eq([1, 2, object, '4'])
end
end
end