LoginSignup
8
6

More than 5 years have passed since last update.

Ruby Array#flattenは階層を指定できる

Posted at
[[[]]].flatten
  # => []

[[[]]].flatten(0)
  # => [[[]]]
[[[]]].flatten(1)
  # => [[]]
[[[]]].flatten(2)
  # => []
[[[]]].flatten(-1)
  # => []

デフォルト値は-1.
ネストが深くなると実害ありそうなので意識する必要がある.

[[1,[2,3]], [4,[5,6]], [7,[8,9]]].flatten
  # => [1, 2, 3, 4, 5, 6, 7, 8, 9]
[[1,[2,3]], [4,[5,6]], [7,[8,9]]].flatten(1)
  # => [1, [2, 3], 4, [5, 6], 7, [8, 9]]

References

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6