Accessing a struct created within a C extension

D

Daniel Berger

Hi all,

Ruby 1.8.2

In pure Ruby I can do this:

class File
TStruct = Struct.new("TStruct",:bar,:baz)
end

t = File::TStruct.new("hello","world")

How do I achieve the equivalent from within a C extension? I have
this:

void Init_file(){
VALUE tStruct = rb_struct_define("TStruct","bar","baz",0);
}

I am unable to access this from within Ruby, however. I also tried
declaring it as "File::TStruct", but Ruby complained with a NameError.
So, how do I declare a struct "under" a specific class?

Regards,

Dan
 
T

ts

D> class File
D> TStruct = Struct.new("TStruct",:bar,:baz)
D> end

D> t = File::TStruct.new("hello","world")

Probably I've not understood

uln% cat a.c
#include <ruby.h>

void Init_a()
{
rb_const_set(rb_cFile, rb_intern("TStruct"),
rb_struct_define("TStruct","bar","baz",0));
}

uln%

uln% ruby -ra -e 'p File::TStruct.new("hello","world")'
#<struct Struct::TStruct bar="hello", baz="world">
uln%



Guy Decoux
 
D

Daniel Berger

ts said:
D> class File
D> TStruct = Struct.new("TStruct",:bar,:baz)
D> end

D> t = File::TStruct.new("hello","world")

Probably I've not understood

uln% cat a.c
#include <ruby.h>

void Init_a()
{
rb_const_set(rb_cFile, rb_intern("TStruct"),
rb_struct_define("TStruct","bar","baz",0));
}

uln%

uln% ruby -ra -e 'p File::TStruct.new("hello","world")'
#<struct Struct::TStruct bar="hello", baz="world">
uln%



Guy Decoux

That's it. Thanks Guy.

Dan.
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top