我无法弄清楚如何在 c 中覆盖 rb_ 函数(如 rb_ivar_get)。我有以下代码:
#include "ruby.h"
void Init_metaobject();
VALUE meta_cObject = Qnil;
VALUE meta_ivar_get(VALUE obj, VALUE mId, VALUE mWarn);
VALUE meta_ivar_set(VALUE obj, VALUE mId, VALUE val);
void Init_metaobject() {
meta_cObject = rb_define_class("MetaObject", rb_cObject);
rb_define_method(meta_cObject, "meta_ivar_get", meta_ivar_get, 2);
rb_define_method(meta_cObject, "meta_ivar_set", meta_ivar_set, 2);
}
VALUE
rb_ivar_get(obj, id)
VALUE obj;
ID id;
{
return meta_ivar_get(obj, ID2SYM(id), Qtrue);
}
VALUE
rb_attr_get(obj, id)
VALUE obj;
ID id;
{
return meta_ivar_get(obj, ID2SYM(id), Qfalse);
}
VALUE
rb_ivar_set(obj, id, val)
VALUE obj;
ID id;
VALUE val;
{
return meta_ivar_set(obj, ID2SYM(id), val);
}
VALUE
meta_ivar_get(obj, mId, mWarn)
VALUE obj;
VALUE mId;
VALUE mWarn;
{
VALUE val;
ID id = rb_to_id(id);
int warn = RTEST(warn);
switch (TYPE(obj)) {
case T_OBJECT:
case T_CLASS:
case T_MODULE:
if (ROBJECT(obj)->iv_tbl && st_lookup(ROBJECT(obj)->iv_tbl, id, &val))
return val;
break;
default:
if (FL_TEST(obj, FL_EXIVAR) || rb_special_const_p(obj))
return generic_ivar_get(obj, id, warn);
break;
}
if (warn) {
rb_warning("instance variable %s not initialized", rb_id2name(id));
}
return Qnil;
}
VALUE
meta_ivar_set(obj, mId, val)
VALUE obj;
VALUE mId;
VALUE val;
{
ID id = rb_to_id(mId);
if (!OBJ_TAINTED(obj) && rb_safe_level() >= 4)
rb_raise(rb_eSecurityError, "Insecure: can't modify instance variable");
if (OBJ_FROZEN(obj)) rb_error_frozen("object");
switch (TYPE(obj)) {
case T_OBJECT:
case T_CLASS:
case T_MODULE:
if (!ROBJECT(obj)->iv_tbl) ROBJECT(obj)->iv_tbl = st_init_numtable();
st_insert(ROBJECT(obj)->iv_tbl, id, val);
break;
default:
generic_ivar_set(obj, id, val);
break;
}
return val;
}
以及以下测试:
require 'metaobject'
class Tracker < MetaObject
attr_accessor :ivar
def initialize
@ivar = nil
end
def meta_ivar_get(symbol, warn)
puts "Instance variable, #{symbol}, retrieved"
super(symbol, warn)
end
def meta_ivar_set(symbol, obj)
puts "Instance variable, #{symbol}, changed to #{obj.inspect}"
super(symbol, obj)
end
end
obj = Tracker.new
obj.ivar = "Modified"
puts obj.ivar
其输出仅为:
Modified
我的想法是 ruby 链接器掩盖了我对 rb_ivar_get、rb_attr_get 和 rb_ivar_set 的定义及其在 variables.c 中的定义。我对吗?如果是这样,我该如何改变它,使我的方法掩盖了红宝石,而不是相反。