create/read/write structs in ruby

Discussion in 'Ruby' started by Michael Hale, Dec 12, 2003.

  1. Michael Hale

    Michael Hale Guest

    I am creating an extension in ruby for a library with a sizeable amount
    of sizeable structs. I would like to be able to create c structs, set
    their attributes, and read their attributes in ruby, for the purpose of
    passing the struct I just setup to a native method that knows what to
    do with it.

    I am envisioning something like this:

    require 'customtype.so'
    t = CustomType.new
    #writer
    t.property1 = 'some value'

    #reader
    t.property1

    Under the covers this would read/write the values on my c struct.
    The only way I can see to implement this would be to basically write a
    c method for every attribute on the struct and then do some method
    missing stuff on the ruby side.

    Is there an easier/better way to read and write data from a struct in
    ruby than what I have outlined?

    Thanks, Michael
     
    Michael Hale, Dec 12, 2003
    #1
    1. Advertising

  2. Michael Hale wrote:
    > I am creating an extension in ruby for a library with a sizeable amount
    > of sizeable structs. I would like to be able to create c structs, set
    > their attributes, and read their attributes in ruby, for the purpose of
    > passing the struct I just setup to a native method that knows what to do
    > with it.
    >
    > I am envisioning something like this:
    >
    > require 'customtype.so'
    > t = CustomType.new
    > #writer
    > t.property1 = 'some value'
    >
    > #reader
    > t.property1
    >
    > Under the covers this would read/write the values on my c struct.
    > The only way I can see to implement this would be to basically write a c
    > method for every attribute on the struct and then do some method missing
    > stuff on the ruby side.
    >
    > Is there an easier/better way to read and write data from a struct in
    > ruby than what I have outlined?


    Take a look at swig (www.swig.org). You can feed it your C/C++ source
    and it will generate the interface code you need to build a ruby
    extension. Some rather large C++ libraries have been wrapped in ruby
    this was (Fox --> FXRuby is a great example).
     
    Joel VanderWerf, Dec 13, 2003
    #2
    1. Advertising

  3. "Michael Hale" <> schrieb im Newsbeitrag
    news:...
    > I am creating an extension in ruby for a library with a sizeable amount
    > of sizeable structs. I would like to be able to create c structs, set
    > their attributes, and read their attributes in ruby, for the purpose of
    > passing the struct I just setup to a native method that knows what to
    > do with it.
    >
    > I am envisioning something like this:
    >
    > require 'customtype.so'
    > t = CustomType.new
    > #writer
    > t.property1 = 'some value'
    >
    > #reader
    > t.property1
    >
    > Under the covers this would read/write the values on my c struct.
    > The only way I can see to implement this would be to basically write a
    > c method for every attribute on the struct and then do some method
    > missing stuff on the ruby side.
    >
    > Is there an easier/better way to read and write data from a struct in
    > ruby than what I have outlined?


    Depending on what you want to do it might be easier to use Struct and do
    the conversion at a later moment. An Example:

    irb(main):007:0> Customer = Struct.new( "Customer", :name, :number )
    => Struct::Customer
    irb(main):008:0> c1 = Customer.new
    => #<struct Struct::Customer name=nil, number=nil>
    irb(main):009:0> c1.name = "foo"
    => "foo"
    irb(main):010:0> c1.number = 123434
    => 123434
    irb(main):011:0> c1
    => #<struct Struct::Customer name="foo", number=123434>
    irb(main):012:0>

    Kind regards

    robert
     
    Robert Klemme, Dec 15, 2003
    #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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    651
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    565
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    651
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,424
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    495
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page