How to do Hash#each_pair from C extension?

B

Bill Kelly

Hi,

I'm trying to copy all the key/value pairs from a Ruby
hash into an STL hash. (BTW, I'm using Ruby 1.8.4 and
cannot change the ruby version easily.)

It looks like rb_hash_foreach() in hash.c does exactly
what I want--allows me to pass in a function pointer,
which it will invoke yielding each key/value pair--but
it's declared static in hash.c.

I looked at duplicating that functionality in my own
C code, but it requires "st.h" as well as constants
such as HASH_DELETED which are defined privately in
hash.c.

Is there a different way I should be going about this?


Thanks for any help,

Bill
 
B

Bill Kelly

From: "Ross Bamford said:
Maybe try something like:

static VALUE hsh_iterfunc(VALUE data_ary, VALUE hsh) {
VALUE key = rb_ary_entry(data_ary, 0);
VALUE value = rb_ary_entry(data_ary, 1);

// do whatever, e.g.
rb_funcall(rb_mKernel, rb_intern("puts"), 2, key, value);

return data_ary;
}


VALUE ruby_iterhsh(VALUE self, VALUE hsh) {
return rb_iterate(rb_each, hsh, hsh_iterfunc, hsh);
}

Thanks! Works great.

What I'm passing in as the second parameter isn't really a VALUE,
it's a pointer to the STL hash. As far as I could tell from grepping
the ruby sources, that *seems* to be legal (some ruby code passes
a NODE* for the second parameter, masquerading as a VALUE.)

(Unless a NODE* really is a VALUE, in which case my passing in
a fake VALUE maybe isn't so good after all. I'm a little uncomfortable
not knowing for sure...)


Regards,

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,200
Latest member
LaraHunley

Latest Threads

Top