Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all keys from ruby hash

Tags:

ruby

In php you can just do $associative_array_keys = array_keys($associative_array)

Is there a function to get all hash keys in ruby?

I'm currently mapping my hashes and returning the keys: my_hash_keys = my_hash.map{|k,v| k}

Is there a better solution?

like image 329
newUserNameHere Avatar asked May 20 '14 17:05

newUserNameHere


1 Answers

You can call .keys on a Hash to get an array of keys back.

See: Hash#keys

like image 101
Cereal Avatar answered Sep 18 '22 23:09

Cereal