Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > C extension: How to create a Hash entry with Symbol key ?

Reply
Thread Tools

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

 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      10-20-2009
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 <>

 
Reply With Quote
 
 
 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      10-21-2009
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 <>

 
Reply With Quote
 
 
 
 
John W Higgins
Guest
Posts: n/a
 
      10-21-2009
On Tue, Oct 20, 2009 at 4:54 PM, I=F1aki Baz Castillo <> wrote=
:

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash key : string or symbol!? Abir B. Ruby 6 04-09-2009 09:48 AM
transform hash key from string into symbol Old Echo Ruby 7 01-02-2008 12:50 PM
hash key to var name of value hash key value Une bévue Ruby 5 08-10-2006 04:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57