Tbpgr Blog

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

Ruby | Hash | rassoc

概要

Hash#rassoc

詳細

Hashから引数と同じ値の要素を探し、配列として返却する

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

h = {
  hoge_key1: :hoge_value,
  hoge_key2: :hoge_value,
  hige_key: :hige_value,
  hage_key: :hage_value,
}

bulk_puts_eval binding, <<-EOS
h.rassoc :hoge_value
h.rassoc :not_exists
EOS
__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils
出力
h.rassoc :hoge_value # => [:hoge_key1, :hoge_value]
nil