我用 ruby2.0.0p0`s fiddle 类去封装一个c的 dll, 当我用 fiddle::CStruct 去封装一个C struct 的时候遇到了一个奇怪的问题。
精简的代码如下:
require "fiddle" require "fiddle/import" require 'fiddle/struct'
types=[Fiddle::TYPE_INT,Fiddle::TYPE_INT] members = ["id","name"]
MyStruct = Fiddle::CStructBuilder.create(Fiddle::CStruct, types, members) obj = MyStruct.allocate
puts obj.methods puts obj.id
我打印了 obj 的 methods, 结果包含了 id 和 id= 这两个方法,可当我调用 id或 id=方法时,它抛出了一个异常 "ruby-2.0.0-p0/lib/ruby/2.0.0/fiddle/struct.rb:59:in block (3 levels) in create': undefined method[]' for nil:NilClass (NoMethodError)" . 我不知道问题出在哪里。