create/read/write structs in ruby

M

Michael Hale

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
 
J

Joel VanderWerf

Michael said:
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).
 
R

Robert Klemme

Michael Hale said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top