Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
迭代Ruby FFI结构布局_Ruby_Rubygems_Ffi - Fatal编程技术网

迭代Ruby FFI结构布局

迭代Ruby FFI结构布局,ruby,rubygems,ffi,Ruby,Rubygems,Ffi,我正在使用非常棒的库来访问ruby中c库中的函数 有没有一种方法可以迭代Ruby FFI::Struct的布局 示例FFI::结构: class Example < FFI::Struct layout :name, string, :desc, :string, :type, :int, :value, :string end 查看一下,我发现可以调用Struct::members来获得定义为“键”的符号数组 从这里,您还可以为

我正在使用非常棒的库来访问ruby中c库中的函数

有没有一种方法可以迭代Ruby FFI::Struct的布局

示例FFI::结构:

class Example < FFI::Struct
  layout :name, string,
         :desc, :string,
         :type, :int,
         :value, :string
end
查看一下,我发现可以调用
Struct::members
来获得定义为“键”的符号数组


从这里,您还可以为每个成员的值获得
Struct::values
,为每个成员的偏移量获得
Struct::offset
,以及一些其他方法。

当然比遍历结构的成员要贵一些,但您也可以使用
将其转换为哈希,以

Foo = Struct.new(:a, :b, :c)
=> Foo
baz = Foo.new(1,2,3)
=> #<struct Foo a=1, b=2, c=3>
baz.to_h
=> {:a=>1, :b=>2, :c=>3}
Foo=Struct.new(:a,:b,:c)
=>Foo
baz=食品新(1,2,3)
=> #
巴兹托什
=>{:a=>1,:b=>2,:c=>3}
Foo = Struct.new(:a, :b, :c)
=> Foo
baz = Foo.new(1,2,3)
=> #<struct Foo a=1, b=2, c=3>
baz.to_h
=> {:a=>1, :b=>2, :c=>3}