Commit f3fb43c7 authored by Christopher Aue's avatar Christopher Aue

Reimplemented Array#flatten with #flatten!

parent ccd55542
...@@ -169,15 +169,9 @@ class Array ...@@ -169,15 +169,9 @@ class Array
# a.flatten(1) #=> [1, 2, 3, [4, 5]] # a.flatten(1) #=> [1, 2, 3, [4, 5]]
# #
def flatten(depth=nil) def flatten(depth=nil)
ar = [] res = dup
self.each do |e| res.flatten! depth
if e.is_a?(Array) && (depth.nil? || depth > 0) res
ar += e.flatten(depth.nil? ? nil : depth - 1)
else
ar << e
end
end
ar
end end
## ##
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment