如何从我的c扩展中访问ruby数组?

时间:2021-03-06 07:12:07

I'm getting this error

我收到了这个错误

ev.c:11: error: subscripted value is neither array nor pointer

for this line

这条线

printf("%d\n", pairs[0][0]);

In this code

在这段代码中

static VALUE EV;
static VALUE PairCounter;

static VALUE 
sort_pairs_2(VALUE self) {
    VALUE pairs;

    pairs = rb_ivar_get(self, rb_intern("pairs"));
    printf("%d\n", pairs[0][0]);
  return Qnil;
}

void Init_ev() {
    rb_eval_string("require './lib/ev/pair_counter'");
    VALUE PairCounter = rb_path2class("EV::PairCounter");
    rb_define_method(PairCounter, "sort_pairs_2", sort_pairs_2, 0);
}

Am I using self incorrectly, and rb_ivar_get is not actually pointing to the PairCounter class?

我是否正确使用自我,并且rb_ivar_get实际上并未指向PairCounter类?

2 个解决方案

#1


3  

I'm pretty sure you need to use the RARRAY_PTR macro on pairs to get at the underlying array; for example, the internal implementation of Array#push (for 1.9.2) looks like this:

我很确定你需要在对上使用RARRAY_PTR宏来获取底层数组;例如,Array#push(for 1.9.2)的内部实现如下所示:

static VALUE
rb_ary_push_1(VALUE ary, VALUE item)
{
    long idx = RARRAY_LEN(ary);

    if (idx >= ARY_CAPA(ary)) {
        ary_double_capa(ary, idx); 
    }
    RARRAY_PTR(ary)[idx] = item;
    ARY_SET_LEN(ary, idx + 1);   
    return ary;
}

The if just sorts out any necessary resizing, then there's RARRAY_PTR(ary)[idx] for accessing a single slot in the array.

if只是排除任何必要的大小调整,然后有RARRAY_PTR(ary)[idx]用于访问数组中的单个插槽。

I don't have any official references to back this up but hopefully this will be of some use.

我没有任何官方参考支持这一点,但希望这将有一些用处。

#2


1  

Ruby arrays are accessed with rb_ functions - not like normal C arrays.

使用rb_函数访问Ruby数组 - 与普通的C数组不同。

Use rb_ary_entry

VALUE rb_ary_entry(VALUE self, long index")

VALUE rb_ary_entry(VALUE self,long index“)

Returns array self's element at index.

返回索引处的数组self元素。

Reference:

http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html

See a list of common Array functions under "Commonly Used Methods".

请参阅“常用方法”下的常用阵列功能列表。

#1


3  

I'm pretty sure you need to use the RARRAY_PTR macro on pairs to get at the underlying array; for example, the internal implementation of Array#push (for 1.9.2) looks like this:

我很确定你需要在对上使用RARRAY_PTR宏来获取底层数组;例如,Array#push(for 1.9.2)的内部实现如下所示:

static VALUE
rb_ary_push_1(VALUE ary, VALUE item)
{
    long idx = RARRAY_LEN(ary);

    if (idx >= ARY_CAPA(ary)) {
        ary_double_capa(ary, idx); 
    }
    RARRAY_PTR(ary)[idx] = item;
    ARY_SET_LEN(ary, idx + 1);   
    return ary;
}

The if just sorts out any necessary resizing, then there's RARRAY_PTR(ary)[idx] for accessing a single slot in the array.

if只是排除任何必要的大小调整,然后有RARRAY_PTR(ary)[idx]用于访问数组中的单个插槽。

I don't have any official references to back this up but hopefully this will be of some use.

我没有任何官方参考支持这一点,但希望这将有一些用处。

#2


1  

Ruby arrays are accessed with rb_ functions - not like normal C arrays.

使用rb_函数访问Ruby数组 - 与普通的C数组不同。

Use rb_ary_entry

VALUE rb_ary_entry(VALUE self, long index")

VALUE rb_ary_entry(VALUE self,long index“)

Returns array self's element at index.

返回索引处的数组self元素。

Reference:

http://ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html

See a list of common Array functions under "Commonly Used Methods".

请参阅“常用方法”下的常用阵列功能列表。