Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over Ruby FFI Struct layout

Tags:

ruby

rubygems

ffi

I am using the really awesome ruby ffi library to access functions in a c library in ruby.

Is there a way to iterate over the layout of a Ruby FFI::Struct?

example FFI::Struct:

class Example < FFI::Struct
  layout :name, string,
         :desc, :string,
         :type, :int,
         :value, :string
end

this doesn't seem to work but something like the below pseudo code:

example_struct.each_key do |key|
  puts key
end
like image 657
hacintosh Avatar asked Feb 14 '26 18:02

hacintosh


1 Answers

Looking at the source for struct.rb, I found that you can call Struct::members to get an array of the symbols you've defined as "keys".

From there, you've also got Struct::values for the values of each member, Struct::offsets for the offsets of each member, and a few other methods.

like image 112
Mark Rushakoff Avatar answered Feb 16 '26 18:02

Mark Rushakoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!