Last active
September 24, 2019 12:03
-
-
Save vin-droid/dac5f05add04d25e455d587568c1e828 to your computer and use it in GitHub Desktop.
flatten in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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