[C ext to Ruby] how to get the key, value pairs of RHASH ???

U

unbewust

i'd like to have, in my module options (is in FileUtils for ex) like
that in the Ruby side:

symt = SYMBOL_TEST.new( "/my/src/path", :verbose => true, :noop =>
true )

i know, from experiment, that, in my C init i get 2 VALUES in that
case the first being a RString the second an RHash.

if i look at ruby.h i get :


struct RHash {
struct RBasic basic;
struct st_table *tbl;
int iter_lev;
VALUE ifnone;
};

i didn't found anything about "struct st_table *tbl" i imagine that's
here the key/value pairs are stored ???

but how to retrieve them ???

generally speaking where could i find examples for each type of
struct ???

because i'll need in the near future the same kind of knoledge about :


struct RRegexp {
struct RBasic basic;
struct re_pattern_buffer *ptr;
long len;
char *str;
};

struct RFile {
struct RBasic basic;
struct OpenFile *fptr;
};

struct RData {
struct RBasic basic;
void (*dmark)(void*);
void (*dfree)(void*);
void *data;
};


best,

Yvon
 
E

Eric Hodel

i'd like to have, in my module options (is in FileUtils for ex) like
that in the Ruby side:

symt = SYMBOL_TEST.new( "/my/src/path", :verbose => true, :noop =>
true )

i know, from experiment, that, in my C init i get 2 VALUES in that
case the first being a RString the second an RHash.

if i look at ruby.h i get :


struct RHash {
struct RBasic basic;
struct st_table *tbl;
int iter_lev;
VALUE ifnone;
};

i didn't found anything about "struct st_table *tbl" i imagine that's
here the key/value pairs are stored ???

but how to retrieve them ???

Use rb_hash_aset and rb_hash_aref from hash.c. Jump down to the
Init_Hash() to get a list of what you can do.
generally speaking where could i find examples for each type of
struct ???

The pickaxe (including first edition) has a section on this. The C
API hasn't changed much.

http://www.rubycentral.com/pickaxe/ext_ruby.html
because i'll need in the near future the same kind of knoledge about :


struct RRegexp {
struct RBasic basic;
struct re_pattern_buffer *ptr;
long len;
char *str;
};
re.c

struct RFile {
struct RBasic basic;
struct OpenFile *fptr;
};

io.c and file.c
struct RData {
struct RBasic basic;
void (*dmark)(void*);
void (*dfree)(void*);
void *data;
};

I think the pickaxe has a section on this.
 
U

unbewust

Use rb_hash_aset and rb_hash_aref from hash.c. Jump down to the
Init_Hash() to get a list of what you can do.


it's OK now for hash keys being strings NOT symbol, i did'nt found a
way to produce a Ruby symbol (as :verbose) from C.

thought i've seen ID2SYM and SYM2ID in ruby.h


also not all of the rb_xxx methods found in hash.c are working from C,
surprisingly ????

for example :

~/work/C/Cext2Ruby/SYMBOL_TEST/ext%> ruby sample.rb
dyld: NSLinkModule() error
dyld: Symbol not found: _rb_hash_keys
Referenced from: ./symbol_test.bundle
Expected in: flat namespace

~/work/C/Cext2Ruby/SYMBOL_TEST/ext%> ruby sample.rb
dyld: NSLinkModule() error
dyld: Symbol not found: _rb_hash_size
Referenced from: ./symbol_test.bundle
Expected in: flat namespace

the Symbol for "rb_hash_keys" or "rb_hash_size" aren't found in my
bundle ???
 
T

Tim Pease

it's OK now for hash keys being strings NOT symbol, i did'nt found a
way to produce a Ruby symbol (as :verbose) from C.

thought i've seen ID2SYM and SYM2ID in ruby.h


also not all of the rb_xxx methods found in hash.c are working from C,
surprisingly ????

If any of the methods in hash.c are preceded by the keyword "static",
then they cannot be called from outside the hash.c file.

for example :

~/work/C/Cext2Ruby/SYMBOL_TEST/ext%> ruby sample.rb
dyld: NSLinkModule() error
dyld: Symbol not found: _rb_hash_keys
Referenced from: ./symbol_test.bundle
Expected in: flat namespace

VALUE keys =3D rb_funcall( hash, rb_intern( "keys" ), 0 );
~/work/C/Cext2Ruby/SYMBOL_TEST/ext%> ruby sample.rb
dyld: NSLinkModule() error
dyld: Symbol not found: _rb_hash_size
Referenced from: ./symbol_test.bundle
Expected in: flat namespace

long size =3D RHASH(hash)->tbl->num_entries;


Blessings,
TwP
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top