I am looking for a function which can get me all the keys from hash or I can loop through the hash to retrieve single key at a time.
Currently I am hardcoding key
VALUE option = rb_hash_aref(options, rb_str_new2("some_key"));
Ruby | Hash key() functionHash#key() is a Hash class method which gives the key value corresponding to the value. If value doesn't exist then return nil.
In Ruby, the key() method of a hash returns the key for the first-found entry with the given value. The given value is passed as an argument to this method.
Short answer is no, hashes need to have unique keys.
You can iterate over the key/value pairs with a callback function using rb_hash_foreach
(blog post w/an example):
void rb_hash_foreach(VALUE, int (*)(ANYARGS), VALUE);
There is an rb_hash_keys
in MRI, but it's not in any header files it seems, so using it may be risky.
You could always make a call to the Ruby method itself:
VALUE keys = rb_funcall(hash, rb_intern("keys"), 0)
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