Ruby 2.7.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Hashクラス > transform_keys
transform_keys {|key| ... } -> Hash
[permalink][rdoc]transform_keys -> Enumerator
すべてのキーに対してブロックを呼び出した結果で置き換えたハッシュを返します。値は変化しません。
h = { a: 1, b: 2, c: 3 }
h.transform_keys {|k| k.to_s } # => {"a"=>1, "b"=>2, "c"=>3}
h.transform_keys(&:to_s) # => {"a"=>1, "b"=>2, "c"=>3}
h.transform_keys.with_index {|k, i| "#{k}.#{i}" }
# => {"a.0"=>1, "b.1"=>2, "c.2"=>3}
[SEE_ALSO] Hash#transform_keys!
[SEE_ALSO] Hash#transform_values
[SEE_ALSO] Hash#transform_values!