C extension: How to create a Hash entry with Symbol key?

Hi, I do know how to create a hash from C in wich keys and values are
strings:

VALUE hash = rb_hash_new();
rb_hash_aset(hash, rb_str_new(“mykey”,5), rb_str_new2(myvalue));

But now I want to create Hash entries with Symbol keys so the resulting
Hash
would look like:
{ :mykey => “lalala” }

By reading the C API documentation I don’t find how to do it:
Ruby C Extension API Documentation (Ruby 1.8) - eqqon

Any help please? Thanks a lot.

El Miércoles, 21 de Octubre de 2009, Iñaki Baz C. escribió:

Hi, I do know how to create a hash from C in wich keys and values are
strings:

VALUE hash = rb_hash_new();
rb_hash_aset(hash, rb_str_new(“mykey”,5), rb_str_new2(myvalue));

But now I want to create Hash entries with Symbol keys so the resulting
Hash would look like:
{ :mykey => “lalala” }

ops, solved right now:

rb_hash_aset(hash, ID2SYM( rb_intern(“mykey”) ),
rb_str_new2(myvalue));

:slight_smile:

On Tue, Oct 20, 2009 at 4:54 PM, Iñaki Baz C. [email protected]
wrote:

This should do it…

rb_hash_aset(hash, ID2SYM(rb_intern(“mykey”)), rb_str_new2(myvalue));

John