Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. # To use this, simply require this file and it will become an extension of the base Array class. As such you can then do
  2. # [1,2,[3]].custom_flatten as in Array.flatten instance method.
  3. class Array
  4. def custom_flatten(array=self)
  5. array.each_with_object([]) do |e, flattened|
  6. flattened.push *(e.is_a?(Array) ? custom_flatten(e) : e)
  7. end
  8. end
  9. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement