2023-05-14 [Ruby]Hashから特定のキーの値を得る
Hash#values_at
hash = { key1: "value1", key2: "value2", key3: "value3" }
values = hash.values_at(:key1, :key2)
p values #=> ["value1", "value2"]
Hash#values_at (Ruby 3.2 リファレンスマニュアル)
in を使う
hash = { key1: "value1", key2: "value2", key3: "value3" }
hash in {key1:, key3:}
puts key1, key3 # value1, value3
=> (右代入)
hash = { key1: "value1", key2: "value2", key3: "value3" }
hash => {key1:, key3:}
puts key1, key3 # value1, value3
変数名を変えることも可能。
hash = { key1: "value1", key2: "value2", key3: "value3" }
hash => {key1: x, key3: y}
puts x, y # value1, value3