Skip to content

Commit

Permalink
[ruby][flatten-array] add note and rename memo variable
Browse files Browse the repository at this point in the history
  • Loading branch information
joaofnds committed Jan 26, 2020
1 parent 82badc1 commit 96a5232
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions ruby/flatten-array/flatten_array.rb
@@ -1,13 +1,18 @@
class FlattenArray
# we can achieve the desired result with `#flatten` and `#compact`
# def self.flatten(array)
# array.flatten.compact
# end

def self.flatten(array)
array.each_with_object([]) do |element, array|
array.each_with_object([]) do |element, memo|
case element
when nil
next
when Array
array.concat flatten(element)
memo.concat flatten(element)
else
array.push element
memo.push element
end
end
end
Expand Down

0 comments on commit 96a5232

Please sign in to comment.