0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

配列を特定数ずつに区切る

Last updated at Posted at 2023-07-11

概要

配列を特定の数ずつに分割したい

環境:Ruby 3.2

方法

each_sliceってのがあった。

sample

# 中身を確認
(1..10).each_slice(3) {|a| p a}
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]

# 配列にして分割されているか確認
(1..10).each_slice(3).to_a
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

# 返却はEnumerator
(1..10).each_slice(3).class
=> Enumerator


0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?