symbols in C extensions

  • Thread starter Paolo Capriotti
  • Start date
P

Paolo Capriotti

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.
 
P

Phil Tomson

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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top