我已经实现了 Ruby C 扩展(即从 ruby 脚本调用 C 函数) 以下是从文件“cFile.c”中用 c 实现的函数
#include<stdio.h>
static VALUE cFunction(VALUE self, VALUE src)
{
if(TYPE(str) == T_STRUCT)
{
printf(" variable str is of STRUCT type \n");
}
// here how can i get the members of structure variable "str"
return Qnil;
}
void Init_MyRuby()
{
VALUE MRuby = rb_define_module("MyRuby");
rb_define_singleton_method(MRuby, "cFunction", cFunction, 1);
}
以下是通过传递结构类型变量调用 functon() 方法的 ruby 脚本代码。客户端.rb:
require 'cFile'
customer = Struct.new( "Customer", :name, :address, :zip )
joe = customer.new( "JoeSmith", "123 Maple, Anytown NC", 12345 )
MyRuby::cFunction(joe)
谁能建议我如何在 cFunction() 中获取结构成员(即名称、地址等)?提前致谢