Accessing a struct created within a C extension

Discussion in 'Ruby' started by Daniel Berger, Feb 1, 2005.

  1. 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
     
    Daniel Berger, Feb 1, 2005
    #1
    1. Advertising

  2. Daniel Berger

    ts Guest

    >>>>> "D" == Daniel Berger <> 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
     
    ts, Feb 1, 2005
    #2
    1. Advertising

  3. ts wrote:
    > >>>>> "D" == Daniel Berger <> 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.
     
    Daniel Berger, Feb 1, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    424
    RA Scheltema
    Jan 6, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,447
    Chris Fogelklou
    Apr 20, 2004
  3. Michael B Allen

    Tail space in struct within struct

    Michael B Allen, Oct 23, 2004, in forum: C Programming
    Replies:
    3
    Views:
    310
    Michael B Allen
    Oct 23, 2004
  4. Daniel Rudy
    Replies:
    7
    Views:
    472
    Daniel Rudy
    Mar 31, 2006
  5. Ufit
    Replies:
    5
    Views:
    334
    John Carson
    Feb 20, 2006
Loading...

Share This Page