Reimplemented Array#flatten with #flatten!

This commit is contained in:
Christopher Aue
2017-08-26 15:19:38 +02:00
parent ccd555421a
commit f3fb43c729
+3 -9
View File
@@ -169,15 +169,9 @@ class Array
# a.flatten(1) #=> [1, 2, 3, [4, 5]]
#
def flatten(depth=nil)
ar = []
self.each do |e|
if e.is_a?(Array) && (depth.nil? || depth > 0)
ar += e.flatten(depth.nil? ? nil : depth - 1)
else
ar << e
end
end
ar
res = dup
res.flatten! depth
res
end
##