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

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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

VALUE hash =3D 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 Has=
h=20
would look like:
{ :mykey =3D> "lalala" }

By reading the C API documentation I don't find how to do it:
http://www.eqqon.com/index.php/Ruby_C_Extension

Any help please? Thanks a lot.


=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
I

Iñaki Baz Castillo

El Mi=C3=A9rcoles, 21 de Octubre de 2009, I=C3=B1aki Baz Castillo escribi=
=C3=B3:
Hi, I do know how to create a hash from C in wich keys and values are
strings:
=20
VALUE hash =3D rb_hash_new();
rb_hash_aset(hash, rb_str_new("mykey",5), rb_str_new2(myvalue));
=20
But now I want to create Hash entries with Symbol keys so the resulting
Hash would look like:
{ :mykey =3D> "lalala" }

ops, solved right now:

rb_hash_aset(hash, ID2SYM( rb_intern("mykey") ), rb_str_new2(myvalue));

:)


=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
J

John W Higgins

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

VALUE hash =3D 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 =3D> "lalala" }
This should do it.....

rb_hash_aset(hash, ID2SYM(rb_intern("mykey")), rb_str_new2(myvalue));

John
 

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,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top