I always end up writing the following code in order to obtain an array of values for a certain key:
hash.map{ |h| h['key'] || "default" }
Does anyone know a cleaner way of writing the same code?
Something like:
hash.map_keys(&:key, 'default')
I always end up writing the following code in order to obtain an array of values for a certain key:
hash.map{ |h| h['key'] || "default" }
Does anyone know a cleaner way of writing the same code?
Something like:
hash.map_keys(&:key, 'default')
You can use hash.keys
to get the hash keys.
To get the the values from an array of hashes for a key you can use fetch if you need to set a default.