Ruby/DL struct inspection?

J

Jim Morris

Hi Ruby/DL experts...

I want to print out the values of the members of a DL struct, is there a
way for the actual instance of the struct to know this?
An example of how I currently do it is below in printStruct, however
this requires that I have both the instance
and the Class name of the struct, then I use the Struct::members method
of the class.

However it would be nice if I could get this all from the instance s
which appears to be type DL::Importable::Internal::Memory and seems to
have no reference to Svn_status_t which is DL::Importable::Internal::Struct

require 'dl/import'
require 'dl/struct'

module Svnrb
extend DL::Importable

Svn_status_t = struct [
"void *entry",
"int text_status",
"int prop_status",
"int locked",
"int copied",
"int switched",
"int repos_text_status",
"int repos_prop_status",
"void *repos_lock"
]

# I want to print the value of each member of s, but I also need to
know the class
# which is hard coded here and shouldn't be
def printStruct(s)
Svn_status_t.members.each do |name|
print " ", name, ": " , eval("s.#{name}"), "\n"
end
end

# pstatus comes from a c routine and is a pointer to a c struct
status= Svn_status_t.new(pstatus)

p status
printStruct(status)

end
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top