# How to do Hash#each\_pair from C extension?

**URL:** <https://www.ruby-forum.com/t/how-to-do-hash-each-pair-from-c-extension/80616>\
**Category:** Ruby\
**Created:** [December 29, 2006, 9:23am UTC](https://www.ruby-forum.com/t/how-to-do-hash-each-pair-from-c-extension/80616 "2006-12-29T09:23:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [December 29, 2006, 9:23am UTC](https://www.ruby-forum.com/t/how-to-do-hash-each-pair-from-c-extension/80616/1 "2006-12-29T09:23:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [December 29, 2006, 2:01pm UTC](https://www.ruby-forum.com/t/how-to-do-hash-each-pair-from-c-extension/80616/2 "2006-12-29T14:01:49Z")

</div>

On Fri, 29 Dec 2006 08:22:34 -0000, Bill K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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?

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);  
}

// … later,

rb\_define\_method(cSomeClass, “iterhsh”, ruby\_iterhsh, 1);

Hope that helps,  
Ross

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [January 3, 2007, 6:07am UTC](https://www.ruby-forum.com/t/how-to-do-hash-each-pair-from-c-extension/80616/3 "2007-01-03T06:07:13Z")

</div>

From: “Ross B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> VALUE value = rb\_ary\_entry(data\_ary, 1);  
> }  
> 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

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [January 3, 2007, 10:35am UTC](https://www.ruby-forum.com/t/how-to-do-hash-each-pair-from-c-extension/80616/4 "2007-01-03T10:35:47Z")

</div>

On Wed, 03 Jan 2007 05:05:21 -0000, Bill K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > // 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.

No problem 🙂

> 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…)

No, that should be fine - ruby guarantees that VALUE casts to void\*, and  
the second argument is purely for your use.

Cheers,
