How to get hash keys - ruby ​​c extension

I'm looking for a function that can get me all the keys from a hash, or I can skip the hash to get one key at a time.

I am currently the key to hard coding

VALUE option = rb_hash_aref(options, rb_str_new2("some_key")); 
+6
source share
2 answers

You can iterate over key / value pairs using the callback function using rb_hash_foreach ( blog post with an example ):

 void rb_hash_foreach(VALUE, int (*)(ANYARGS), VALUE); 

There are rb_hash_keys in the MRI , but this is not in any header files, so using this can be risky.

+1
source

You can always call the Ruby method itself:

 VALUE keys = rb_funcall(hash, rb_intern("keys"), 0) 
0
source

Source: https://habr.com/ru/post/979408/


All Articles