Skip to content

Instantly share code, notes, and snippets.

@aashish
Created May 13, 2019 15:24
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 aashish/e70c18af404f4be5537e187671580307 to your computer and use it in GitHub Desktop.
Save aashish/e70c18af404f4be5537e187671580307 to your computer and use it in GitHub Desktop.
Array of arrays will be flattened.
class Array
def flattener
arr = []
self.each do |x|
if x.is_a? Array
arr = arr + x.flattener
else
arr << x
end
end
return arr
end
end
p [3, [4, [5, 6]], 7].flattener
#ouput [3, 4, 5, 6, 7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment