Advertisement
Guest User

Untitled

a guest
May 27th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.36 KB | None | 0 0
  1. class Array
  2. def custom_flatten
  3. flattened = []
  4. each do |elem|
  5. flattened += elem.is_a?(Array) ? elem.custom_flatten : [elem]
  6. end
  7. flattened
  8. end
  9. end
  10.  
  11. RSpec Array
  12. describe "#custom_flatten"
  13. let(:ar) { [1,2, [3,4, 5], 5] }
  14.  
  15. it "flattens the array into a single one" do
  16. expect(ar.custom_flatten).to eq([1,2,3,4,5,5])
  17. end
  18. end
  19. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement