Today I Learned
by OneCode

Flatten method can take an argument

I was working with multi-dimensional arrays. I found a problem - I needed to remove one level of the nested array. How to do it with a single method?

I took a look at the flatten definition, and I found that it can take addition argument - level. If you call flatten without additional arguments, it will flatten an array recursively. But you can specify how many array levels should be “removed” by using the additional argument.

[[1,[2,3]], 4].flatten
#=> [1,2,3,4]

[[1,[2,3]], 4].flatten(1)
#=> [1,[2,3],4]