Type casting for Hash

How to cast a Hash(String, TOML::Type) to Hash(String, String)? The usual #map function I am using for Array, doesn’t work for Hash.

This looks like exactly the use case for Hash#transform_values (docs). I’m not familiar with whatever TOML shard you’re using, so I can’t give exact code, but it might be something like this:

string_hash = toml_hash.transform_values { |toml_type_value| toml_type_value.type_name }

(Depending on how complex the transformation to String is, you might just be able to do &.to_s instead of the block above.)

Thanks. That’s exactly what I was looking for.

1 Like