Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > symbols in C extensions

Reply
Thread Tools

symbols in C extensions

 
 
Paolo Capriotti
Guest
Posts: n/a
 
      08-03-2005
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.


 
Reply With Quote
 
 
 
 
rmagick@gmail.com
Guest
Posts: n/a
 
      08-03-2005
Try this:

ID2SYM(rb_intern("my_symbol"))

 
Reply With Quote
 
 
 
 
Brian Schröder
Guest
Posts: n/a
 
      08-03-2005
On 03/08/05, Paolo Capriotti <(E-Mail Removed)> 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/


 
Reply With Quote
 
Phil Tomson
Guest
Posts: n/a
 
      08-03-2005
In article <(E-Mail Removed)>,
Brian Schröder <(E-Mail Removed)> wrote:
>On 03/08/05, Paolo Capriotti <(E-Mail Removed)> 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
 
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
Unable to access extensions menu or add extensions (I've tried past suggestions) nospam@spam.com Firefox 8 12-01-2005 01:33 AM
Displaying Special Symbols Gibs ASP .Net 3 06-29-2004 07:33 PM
cisco router/switch symbols dave-au Cisco 0 02-15-2004 10:01 PM
Debug symbols not for all .aspx forms. =?Utf-8?B?QUs=?= ASP .Net 1 01-23-2004 11:48 AM
Pyhon Extensions linker error, unresolved symbols Jim Python 0 11-18-2003 11:13 PM



Advertisments