Tbpgr Blog

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

Ruby | Hash | values_at

概要

Hash#values_at

詳細

Hashの任意のkeyに該当する要素を全て取得する

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

h = {
  hoge_key1: '1',
  hoge_key2: '2',
  hige_key: '3',
  hage_key: '4',
}

bulk_puts_eval binding, <<-EOS
h.values_at :hoge_key1, :hoge_key2
EOS
__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval
attr_accessor_init

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils
出力
h.values_at :hoge_key1, :hoge_key2 # => ["1", "2"]