Can someone please guide me to get the key/value pairs in C
extension?
You use functions rb_hash_foreach, rb_hash_aref and friends. I believe
the best thing to do to learn how to use this function is to look at
the many examples in the MRI source code - especially in the
extensions:
I have browsed through the code in ext directory as you guided but I
have not found the case where class contains a hash map and is declared
as attr_accessor.
I am using the following
#In Ruby
class AA
Anchor =
{
anchorAA: 0,
anchorAB: 1,
anchorAC: 2
}
/On C side*/
VALUE func (VALUE self, VALUE obj)
{
/SAY if self corresponds to object of class AA/
VALUE test = rb_iv_get(self, “@Anchor”);
ID id = rb_intern(“anchorAA”);
VALUE test1 = rb_hash_aref(test, ID2SYM(id));
I have no idea what that is supposed to do or how it is supposed to
work. You need to make your code work in pure ruby before you bother
trying to make it work in C.
Finally, please read all of README.EXT in the ruby source.
Is the above correct Carlo?
“@Anchor” means nothing. Anchor is a class constant, not an instance
variable.
The function to access class constants from C is rb_const_get. Search
for examples in the same way.
I personally never needed to access ruby class constants from C. I try
to make as neat a separation between the two worlds as possible. I use
C only when I need raw speed, or when I have to access C libraries.
If I were you, I’d pass all info I need to transfer from Ruby to C as
separate parameters. It is nifty to be ruby=ish under C, but it
obfuscates things a lot.
Carlo
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.