概要
二次元配列の各先頭の任意のindexに値を追加する
詳細
tbpgr_utils gemのArray#together_insert(別名でtinsertもあり)
二次元配列の各先頭の任意のindexに値を追加します。
index指定を負の数にすると、Array#insertと同様に後ろから数えた位置に挿入します。
範囲外の値を指定すると、間をnilで埋めます。
事前準備
gem install tbpgr_utils
主な用途
二次元配列の各先頭の任意のindexに値を追加したい場合。
サンプルコード
# encoding: utf-8 require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS [[*1..5], [*5..9]].together_insert 2, 99 [[*1..5], [*6..10]].tinsert -2, 99 [[*1..5], [*6..10]].tinsert 6, 11, 22, 33 EOS __END__ 下記はTbpgrUtils gemの機能 bulk_puts_eval https://rubygems.org/gems/tbpgr_utils https://github.com/tbpgr/tbpgr_utils
出力
[[*1..5], [*5..9]].together_insert 2, 99 # => [[1, 2, 99, 3, 4, 5], [5, 6, 99, 7, 8, 9]] [[*1..5], [*6..10]].tinsert -2, 99 # => [[1, 2, 3, 4, 99, 5], [6, 7, 8, 9, 99, 10]] [[*1..5], [*6..10]].tinsert 6, 11, 22, 33 # => [[1, 2, 3, 4, 5, nil, 11, 22, 33], [6, 7, 8, 9, 10, nil, 11, 22, 33]]