Skip to content

Instantly share code, notes, and snippets.

@shuklaneerajdev
Created May 20, 2016 20:58
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 shuklaneerajdev/9fb5af4adb875b413d3bc9e6cc312859 to your computer and use it in GitHub Desktop.
Save shuklaneerajdev/9fb5af4adb875b413d3bc9e6cc312859 to your computer and use it in GitHub Desktop.
def flatten(input)
result = Array.new
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
# else this is just a regular element and add the element to result
result << element
end
end
return result
end
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