Skip to content

Instantly share code, notes, and snippets.

@shklnrj shklnrj/flatten.rb

Created May 20, 2016
What would you like to do?
def flatten(input)
result =
input.each do |element|
if element && element.instance_of?(Array)
# if the current element is an array then recursively flatten that
# array and add that array to the result
result = result+flatten(element)
# else this is just a regular element and add the element to result
result << element
return result
input = [[1,2,[3]],4]
print flatten(input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.