# Q: hash parameter passed to c++ ruby extension is incomplete

**URL:** <https://www.ruby-forum.com/t/q-hash-parameter-passed-to-c-ruby-extension-is-incomplete/162552>\
**Category:** Ruby\
**Created:** [March 11, 2009, 9:20am UTC](https://www.ruby-forum.com/t/q-hash-parameter-passed-to-c-ruby-extension-is-incomplete/162552 "2009-03-11T09:20:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yurid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yurid](https://www.ruby-forum.com/u/yurid)\
**Post date:** [March 11, 2009, 9:20am UTC](https://www.ruby-forum.com/t/q-hash-parameter-passed-to-c-ruby-extension-is-incomplete/162552/1 "2009-03-11T09:20:22Z")

</div>

I’m trying to pass following hash to my c++ ruby extension:  
h1 = {“G804988.19” =\> 0, “G804988.21” =\> 0, “NH3812.06” =\> 0,  
“NH3812.10” =\> 0}  
myext.SomeFunction(h1)

The hash looks fine from script:  
h1.each\_pair {| key, value | printf("%s - %s\n", key, value) }

The above statement shows that all 4 hash entries are there.

But in the extension I can only see 3 items.  
Code is like below:

extern “C” VALUE SomeFunction(VALUE vSelf,VALUE vh1)  
{  
struct st\_table \*pTblHash;

pTblHash = RHASH(vh1)-\>tbl;  
}

The pTblHash-\>num\_entries is correct and equals to 4.  
But in the pTblHash-\>bins array there are only 3 not empty pointers to  
hash elements.

If I use one symbol shorter hash keys for one of entries (G80498.21  
instead of G804988.21), for example:  
h1 = {“G804988.19” =\> 0, “G80498.21” =\> 0, “NH3812.06” =\> 0, “NH3812.10”  
=\> 0}

then problem disappears.

I’m debugging under Windows XP and Ruby 1.8.

I’ll appreciate any help.  
Thank you.  
Yuri

---

<div class="post-metadata">

**Author:** ![yurid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yurid](https://www.ruby-forum.com/u/yurid)\
**Post date:** [March 11, 2009, 10:18am UTC](https://www.ruby-forum.com/t/q-hash-parameter-passed-to-c-ruby-extension-is-incomplete/162552/2 "2009-03-11T10:18:29Z")

</div>

Hi,

2009/3/11 Yuri D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> The pTblHash-\>num\_entries is correct and equals to 4.  
> I’m debugging under Windows XP and Ruby 1.8.

I guess the problem is due to the Collision resolution [1].

The st\_table, num\_entries and bins are highly implementation dependent  
so using it directly in extension code is not recommended.

In my experience, almost all extension codes required hash could be  
done with following rb\_hash\_xxx functions  
rb\_hash\_new  
rb\_hash\_delete  
rb\_hash\_foreach  
rb\_hash\_aref  
rb\_hash\_aset

[1] [Hash table - Wikipedia](http://en.wikipedia.org/wiki/Hash_table#Collision_resolution)

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![yurid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/57b2e6/32.png) [@yurid](https://www.ruby-forum.com/u/yurid)\
**Post date:** [March 11, 2009, 10:42am UTC](https://www.ruby-forum.com/t/q-hash-parameter-passed-to-c-ruby-extension-is-incomplete/162552/3 "2009-03-11T10:42:17Z")

</div>

Heesob P. wrote:

> I guess the problem is due to the Collision resolution [1].
> 
> The st\_table, num\_entries and bins are highly implementation dependent  
> so using it directly in extension code is not recommended.
> 
> rb\_hash\_foreach

Thank you  
Actually I started with using rb\_hash\_foreach, but sometimes I don’t see  
this function in intern.h (probably older Ruby versions), so I turned to  
direct usage.  
But after your reply I thought that maybe there are two items under same  
bin and I can reach it somehow.
