Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | 二次元配列のそれぞれの配列からpopする

概要

二次元配列のそれぞれの配列からpopする

詳細

tbpgr_utils gemのArray#together_pop(別名でtpopもあり)
二次元配列のそれぞれの配列からpopします。

事前準備
gem install tbpgr_utils
主な用途

二次元配列のそれぞれの配列からpopしたい場合。

サンプルコード
# encoding: utf-8
require 'tbpgr_utils'

list = [[*1..5], [*6..10]]
print list.together_pop, "\n"
print list, "\n"
puts
list = [[*1..5], []]
print list.together_pop, "\n"
print list, "\n"
puts
list = [[*1..5], [*6..10]]
print list.together_pop(2), "\n"
print list, "\n"
puts
出力
[5, 10]
[[1, 2, 3, 4], [6, 7, 8, 9]]

[5, nil]
[[1, 2, 3, 4], []]

[[4, 5], [9, 10]]
[[1, 2, 3], [6, 7, 8]]