symbols in C extensions

Discussion in 'Ruby' started by Paolo Capriotti, Aug 3, 2005.

  1. Is there a better (i.e. faster) way to create a symbol in a C
    extension than writing: rb_eval_string(":my_symbol")?

    Thanks,

    Paolo.
    Paolo Capriotti, Aug 3, 2005
    #1
    1. Advertising

  2. Paolo Capriotti

    Guest

    Try this:

    ID2SYM(rb_intern("my_symbol"))
    , Aug 3, 2005
    #2
    1. Advertising

  3. On 03/08/05, Paolo Capriotti <> wrote:
    > Is there a better (i.e. faster) way to create a symbol in a C
    > extension than writing: rb_eval_string(":my_symbol")?
    >=20
    > Thanks,
    >=20
    > Paolo.
    >=20
    >=20


    From README.EXT:

    :Identifier

    You can get the symbol value from a string within C code by using

    rb_intern(const char *name)

    regards,

    Brian


    --=20
    http://ruby.brian-schroeder.de/

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
    Brian Schröder, Aug 3, 2005
    #3
  4. Paolo Capriotti

    Phil Tomson Guest

    In article <>,
    Brian Schröder <> wrote:
    >On 03/08/05, Paolo Capriotti <> wrote:
    >> Is there a better (i.e. faster) way to create a symbol in a C
    >> extension than writing: rb_eval_string(":my_symbol")?
    >>=20
    >> Thanks,
    >>=20
    >> Paolo.
    >>=20
    >>=20

    >
    >From README.EXT:
    >
    > :Identifier
    >
    >You can get the symbol value from a string within C code by using
    >
    > rb_intern(const char *name)
    >
    >regards,
    >
    >Brian
    >
    >
    >--=20
    >http://ruby.brian-schroeder.de/
    >
    >Stringed instrument chords: http://chordlist.brian-schroeder.de/
    >
    >


    Also, to speed things up in cases where you'll need to use the same
    symbols over and over you can do the rb_intern in the Init function of
    your extension, for example:

    #include "ruby.h"
    #include <math.h>
    #include <stdio.h>

    static int id_x;
    static int id_y;
    static int id_equal;


    .... a lot of snippage ...

    VALUE cACOMod;
    VALUE cACOPoint;
    VALUE cACOGraph;


    void Init_ACO_Ext() {
    printf("ACO_Ext initializing...\n");
    cACOMod = rb_define_module("ACO");
    cACOPoint = rb_define_class_under(cACOMod,"Point",rb_cObject);
    cACOGraph = rb_define_class_under(cACOMod,"Graph",rb_cObject);
    rb_define_alloc_func(cACOPoint, aco_point_alloc);
    rb_define_method(cACOPoint,"initialize",aco_point_init,2);
    rb_define_method(cACOPoint,"-",aco_point_diff,1);
    rb_define_method(cACOPoint,"==",aco_point_equal,1);
    rb_define_method(cACOPoint,"x",aco_point_getx,0);
    rb_define_method(cACOPoint,"y",aco_point_gety,0);

    //create your symbols ahead of time here:
    id_x = rb_intern("x");
    id_y = rb_intern("y");
    id_equal = rb_intern("==");
    }



    Seems like it could help save some time if you use the symbols lots of
    times in your accessor functions for example.

    Phil
    Phil Tomson, Aug 3, 2005
    #4
    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. =?Utf-8?B?QUs=?=

    Debug symbols not for all .aspx forms.

    =?Utf-8?B?QUs=?=, Jan 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    896
    Habib Heydarian [MSFT]
    Jan 23, 2004
  2. Gibs

    Displaying Special Symbols

    Gibs, Jun 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    456
    Ken Cox [Microsoft MVP]
    Jun 29, 2004
  3. Brian Henry

    <% symbols

    Brian Henry, Jul 6, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    575
    clintonG
    Jul 6, 2004
  4. Shawn South
    Replies:
    0
    Views:
    3,419
    Shawn South
    Aug 16, 2004
  5. Jim
    Replies:
    0
    Views:
    323
Loading...

Share This Page