Tbpgr Blog

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

Ruby | 指定した値が二次元配列に含まれているか確認する

概要

指定した値が二次元配列に含まれているか確認する

詳細

tbpgr_utils gemのArray#together_include?(別名でtinclude?もあり)
指定した値が二次元配列に含まれているか確認します。
デフォルトでは、複数ある配列のどれか1つに含まれていればtrueを返却します。
個別に含まれているかどうか判定する場合は、第2引数にtrueを指定して呼び出します。
結果は真偽値の配列で返却されます。

事前準備
gem install tbpgr_utils
主な用途

指定した値が二次元配列に含まれているか確認したい場合。

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

bulk_puts_eval binding, <<-EOS
[[*1..5], [*5..10]].together_include? 1
[[*1..5], [*5..10]].together_include? 5
[[*1..5], [*5..10]].together_include? 11
[[*1..5], [*5..10]].together_include? 1, true
[[*1..5], [*5..10]].together_include? 5, true
[[*1..5], [*5..10]].together_include? 11, true
[[*1..5], [*5..10]].tinclude? 1
[[*1..5], [*5..10]].tinclude? 5
[[*1..5], [*5..10]].tinclude? 11
[[*1..5], [*5..10]].tinclude? 1, true
[[*1..5], [*5..10]].tinclude? 5, true
[[*1..5], [*5..10]].tinclude? 11, true
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils
出力
[[*1..5], [*5..10]].together_include? 1 # => true
[[*1..5], [*5..10]].together_include? 5 # => true
[[*1..5], [*5..10]].together_include? 11 # => false
[[*1..5], [*5..10]].together_include? 1, true # => [true, false]
[[*1..5], [*5..10]].together_include? 5, true # => [true, true]
[[*1..5], [*5..10]].together_include? 11, true # => [false, false]
[[*1..5], [*5..10]].tinclude? 1 # => true
[[*1..5], [*5..10]].tinclude? 5 # => true
[[*1..5], [*5..10]].tinclude? 11 # => false
[[*1..5], [*5..10]].tinclude? 1, true # => [true, false]
[[*1..5], [*5..10]].tinclude? 5, true # => [true, true]
[[*1..5], [*5..10]].tinclude? 11, true # => [false, false]