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