Skip to content

Instantly share code, notes, and snippets.

@vin-droid
Last active September 24, 2019 12:03
Show Gist options
  • Save vin-droid/dac5f05add04d25e455d587568c1e828 to your computer and use it in GitHub Desktop.
Save vin-droid/dac5f05add04d25e455d587568c1e828 to your computer and use it in GitHub Desktop.
flatten in ruby
## Q-2 Write a method which will flatten an Array?
arr = [1, [2, 3], [4]]
def flatten(arr)
flatten_arry = []
arr.each do |item|
if item.class == Array
flatten_arry += flatten(item)
else
flatten_arry << item
end
end
flatten_arry
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment