# Any "easy" example of using T\_HASH in a C extension?

**URL:** <https://www.ruby-forum.com/t/any-easy-example-of-using-t-hash-in-a-c-extension/177162>\
**Category:** Ruby\
**Created:** [October 12, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/any-easy-example-of-using-t-hash-in-a-c-extension/177162 "2009-10-12T18:25:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 12, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/any-easy-example-of-using-t-hash-in-a-c-extension/177162/1 "2009-10-12T18:25:28Z")

</div>

Hi, I’ve found some tutorials explaining how to create and manipulat a  
Ruby  
array from a C extension using T\_ARRAY and its realted functions.  
However I find nothing about T\_HASH, and trying to extract the functions  
from  
Ruby “hash.c” code is… not very cool.

Is there any tutorial about creating a simple hash in Ruby C extension?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 12, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/any-easy-example-of-using-t-hash-in-a-c-extension/177162/2 "2009-10-12T19:06:27Z")

</div>

On Oct 12, 2009, at 9:24 AM, Iñaki Baz C. wrote:

> Hi, I’ve found some tutorials explaining how to create and manipulat  
> a Ruby  
> array from a C extension using T\_ARRAY and its realted functions.  
> However I find nothing about T\_HASH, and trying to extract the  
> functions from  
> Ruby “hash.c” code is… not very cool.

Sure it is! All of the hipsters at Beauty Bar were talking about it  
the other night. But you’d better hop on board quickly before it  
becomes uncool, otherwise you’re going to have to wait until it  
becomes “retro cool”.

> Is there any tutorial about creating a simple hash in Ruby C  
> extension?

rb\_hash\_new(void)  
rb\_hash\_aset(VALUE hash, VALUE key, VALUE val)  
rb\_hash\_aref(VALUE hash, VALUE key)

Though, if you’re just doing hash manipulation, why not just do it in  
Ruby?

* * *

Aaron P.  
[http://tenderlovemaking.com](http://tenderlovemaking.com)

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 12, 2009, 7:10pm UTC](https://www.ruby-forum.com/t/any-easy-example-of-using-t-hash-in-a-c-extension/177162/3 "2009-10-12T19:10:33Z")

</div>

El Lunes, 12 de Octubre de 2009, Aaron P.  
escribió:\> becomes uncool, otherwise you’re going to have to wait until it

> becomes “retro cool”.
> 
> > Is there any tutorial about creating a simple hash in Ruby C  
> > extension?
> 
> rb\_hash\_new(void)  
> rb\_hash\_aset(VALUE hash, VALUE key, VALUE val)  
> rb\_hash\_aref(VALUE hash, VALUE key)

Yeah! just found these functions in an online presentation. 🙂

> Though, if you’re just doing hash manipulation, why not just do it in  
> Ruby?

Not exactly. I’ve coded a parser in C and it must return a Ruby hash.

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 12, 2009, 7:26pm UTC](https://www.ruby-forum.com/t/any-easy-example-of-using-t-hash-in-a-c-extension/177162/4 "2009-10-12T19:26:22Z")

</div>

On 12 Oct 2009, at 18:24, Iñaki Baz C. wrote:

> Hi, I’ve found some tutorials explaining how to create and manipulat  
> a Ruby  
> array from a C extension using T\_ARRAY and its realted functions.  
> However I find nothing about T\_HASH, and trying to extract the  
> functions from  
> Ruby “hash.c” code is… not very cool.

I’d advise against using T\_ARRAY, RARRAY\_PTR etc, writing stuff with  
the following methods makes life a lot easier for other Ruby  
implementations that also support MRI C extensions:

rb\_ary\_new(void);  
rb\_ary\_push(VALUE array, VALUE element);  
rb\_ary\_entry(VALUE array, long i);  
rb\_ary\_store(VALUE array, long i, VALUE element);

–  
Regards,

Dirkjan Bussink

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [October 12, 2009, 7:39pm UTC](https://www.ruby-forum.com/t/any-easy-example-of-using-t-hash-in-a-c-extension/177162/5 "2009-10-12T19:39:50Z")

</div>

El Lunes, 12 de Octubre de 2009, Dirkjan Bussink  
escribió:\> implementations that also support MRI C extensions:

> rb\_ary\_new(void);  
> rb\_ary\_push(VALUE array, VALUE element);  
> rb\_ary\_entry(VALUE array, long i);  
> rb\_ary\_store(VALUE array, long i, VALUE element);

Yes, finally I used just “rb\_hash\_new” and “rb\_hash\_aset” functions.
