Tbpgr Blog

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

Ruby | 二次元配列の各先頭の値のindexを取得する

概要

二次元配列の各先頭の値のindexを取得する

詳細

tbpgr_utils gemのArray#together_index(別名でtindexもあり)
二次元配列の各先頭の値のindexを取得します。

事前準備
gem install tbpgr_utils
主な用途

二次元配列の各先頭の値のindexを取得したい場合。

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

bulk_puts_eval binding, <<-EOS
[[*1..5], [*5..9]].together_index 5
[[*1..5], [*6..10]].tindex 9
[[*1..5], [*6..10]].tindex 11
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils
出力
[[*1..5], [*5..9]].together_index 5 # => [4, 0]
[[*1..5], [*6..10]].tindex 9 # => [nil, 3]
[[*1..5], [*6..10]].tindex 11 # => [nil, nil]