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

Discussion in 'Ruby' started by Iñaki Baz Castillo, Oct 21, 2009.

  1. 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 <>
     
    Iñaki Baz Castillo, Oct 21, 2009
    #1
    1. Advertising

  2. Re: C extension: How to create a Hash entry with Symbol key ?[SOLVED]

    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 <>
     
    Iñaki Baz Castillo, Oct 21, 2009
    #2
    1. Advertising

  3. 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
     
    John W Higgins, Oct 21, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M P
    Replies:
    1
    Views:
    560
  2. rp
    Replies:
    1
    Views:
    597
    red floyd
    Nov 10, 2011
  3. Une bévue
    Replies:
    5
    Views:
    179
    Une bévue
    Aug 10, 2006
  4. Old Echo
    Replies:
    7
    Views:
    147
    Trans
    Jan 2, 2008
  5. Abir B.

    Hash key : string or symbol!?

    Abir B., Apr 9, 2009, in forum: Ruby
    Replies:
    6
    Views:
    186
    Andrew Timberlake
    Apr 9, 2009
Loading...

Share This Page