What is the "struct" in the documentation of Ruby Fiddle?

68 views Asked by At

I had used Ruby 1.9 long ago and now I am trying Ruby 3.2 Fiddle. I tried the following snippet in the README.md, but it raised a NameError on the struct.

StudentCollegeDetail = struct [
  'int college_id',
  'char college_name[50]'
]

How can I use that struct? Where is the documentation of it?

1

There are 1 answers

3
relent95 On

Well, it was a stupid question. You can use the struct like this.

require 'fiddle/import'

StudentCollegeDetail = Fiddle::Importer::struct [
  'int college_id',
  'char college_name[50]'
]

The documentation is at this.

As commented by Holger, the Fiddle::Importer is designed to be used as an argument of the extend() inside a module like this.

require 'fiddle/import'

module Libc
  extend Fiddle::Importer
  dlload 'libc.so.6'
  typealias 'clockid_t', 'int'
  typealias 'time_t', 'int64_t'
  Timespec = struct [
    'time_t   tv_sec',
    'long     tv_nsec',
  ]
  extern 'int clock_getres(clockid_t clk_id, struct timespec *res);'
end

res = Libc::Timespec.malloc
Libc.clock_getres(Process::CLOCK_REALTIME, res)
p res.tv_nsec