So with a normal hash you can use this to get the keys:
hash.keys
How can I get the keys of the 2nd dimension of a multidimensional hash that looks like this:
{"<id>"=>{"first_name"=>"test", "last_name"=>"test_l", "username"=>"test_user",
"title"=>"Sales Manager", "office"=>"test", "email"=>"[email protected]"}}
<id>
is unique for each item.
So the keys I want from above are: first_name, last_name, username, title, office and email
You can find a key that leads to a certain value with Hash#key . If you are using a Ruby earlier than 1.9, you can use Hash#index . Once you have a key (the keys) that lead to the value, you can compare them and act on them with if/unless/case expressions, custom methods that take blocks, et cetera.
Overview. We can check if a particular hash contains a particular key by using the method has_key?(key) . It returns true or false depending on whether the key exists in the hash or not.
Ruby | Hash delete() function delete() is an Hash class method which deletes the key-value pair and returns the value from hash whose key is equal to key. Return: value from hash whose key is equal to deleted key.
You would do something like:
hash["<id>"].keys
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With