[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Accessing a struct created within a C extension

djberg96

2/1/2005 5:05:00 PM

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

2 Answers

ts

2/1/2005 5:11:00 PM

0

>>>>> "D" == Daniel Berger <djberg96@hotmail.com> writes:

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


djberg96

2/1/2005 5:13:00 PM

0


ts wrote:
> >>>>> "D" == Daniel Berger <djberg96@hotmail.com> writes:
>
> 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.