Create/read/write structs in ruby

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 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).

“Michael Hale” michael@halefamilysite.com schrieb im Newsbeitrag
news:542265F2-2CF8-11D8-BE7E-000A95C4BF56@halefamilysite.com

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
=> #
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