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"));
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"));
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.
candidate function not viable: no known conversion from 'int (VALUE, VALUE, VALUE)' to 'int (*)(...)' for 2nd argument void rb_hash_foreach(VALUE, int (*)(ANYARGS), VALUE);
–
Brittaniebrittany rb_hash_keys
: rxr.whitequark.org/mri/source/hash.c#1627 –
Allanallana candidate function not viable: no known conversion from 'int (VALUE, VALUE, VALUE)' to 'int (*)(...)' for 2nd argument void rb_hash_foreach(VALUE, int (*)(ANYARGS), VALUE);
This is the code I am trying ` VALUE ary; ary = rb_ary_new(); rb_hash_foreach(opts, convert_keys_to_a, ary);` static int convert_keys_to_a(VALUE key, VALUE value, VALUE ary) { if (logic) return ST_CONTINUE; if (logic { rb_hash_foreach(value, convert_keys_to_a, ary); } return rb_ary_push(ary, key); }
–
Brittaniebrittany // Force inclusion of hash declarations (only MRI includes by default) #ifdef HAVE_RUBY_ST_H #include "ruby/st.h" #else #include "st.h" #endif
–
Wirth You could always make a call to the Ruby method itself:
VALUE keys = rb_funcall(hash, rb_intern("keys"), 0)
© 2022 - 2024 — McMap. All rights reserved.
rb_hash_keys
complainsuse of undeclared identifier 'rb_hash_keys'
– Brittaniebrittany