From: gmspro Date: 2012-06-23T15:08:24+09:00 Subject: [ruby-core:45790] How can i access ruby array element in C from ruby source code array.c? ---1624034222-1066378124-1340431702=:97157 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable =0A Hi, I'm trying to get the value of a array from ruby source code= , That is array.c ./ruby arr=3DArray.new(3) arr[0]=3D5 arr[1]=3D3 arr[2]=3D2 =0A=0AThese lines should call this function, https://github.com/ruby/ruby/b= lob/trunk/array.c#L568 according to this, http://www.ruby-doc.org/core-1.9.= 3/Array.html =0A=0ASo I have added couple of lines there to display the values of array= . But i didn't get the expected result.=0A=0Aelse { =A0 =A0 memfill(RARRAY_PTR(ary), len, val); =A0 =A0 ARY_SET_LEN(ary, len); =A0 =A0 int i; =A0 =A0 int result; =A0 =A0 result =3D 0; =A0 =A0 VALUE *s_arr =3D RARRAY_PTR(ary); =A0 =A0 for(i =3D 0; i < len; i++) { =A0 =A0 =A0 =A0 result =3D LONG2NUM(s_arr[i]); =A0 =A0 =A0 =A0 printf("r: %d\n",result); =A0 =A0 } } =0A=0AI got the result like this:=0A=0Aarr=3DArray.new(3) arr[0]=3D5 arr[1]=3D3 arr[2]=3D2 r: 9 r: 9 r: 9 =0A=0AWhy is this result? Why 9?=0A=0AI have followed these to solve it:=0A= =0AHow to convert ruby array to C array with RubyInline?https://github.com/= ruby/ruby/blob/trunk/README.EXT#L131https://github.com/ruby/ruby/blob/trunk= /include/ruby/ruby.h#L708https://github.com/ruby/ruby/blob/trunk/include/ru= by/ruby.h#L731=0A=0A I also try editing/customizing array.c:rb_ary_aset function like this: static VALUE rb_ary_aset(int argc, VALUE *argv, VALUE ary) { =A0=A0=A0 long offset, beg, len; =A0=A0=A0 if (argc =3D=3D 3) { =A0=A0=A0 =A0=A0=A0 rb_ary_modify_check(ary); =A0=A0=A0 =A0=A0=A0 beg =3D NUM2LONG(argv[0]); =A0=A0=A0 =A0=A0=A0 len =3D NUM2LONG(argv[1]); =A0=A0=A0 =A0=A0=A0 rb_ary_splice(ary, beg, len, argv[2]); =A0=A0=A0 =A0=A0=A0 return argv[2]; =A0=A0=A0 } =A0=A0=A0 if (argc !=3D 2) { =A0=A0=A0 =A0=A0=A0 rb_raise(rb_eArgError, "wrong number of arguments (%d f= or 2)", argc); =A0=A0=A0 } =A0=A0=A0 rb_ary_modify_check(ary); =A0=A0=A0 if (FIXNUM_P(argv[0])) { =A0=A0=A0 =A0=A0=A0 offset =3D FIX2LONG(argv[0]); =A0=A0=A0 =A0=A0=A0 goto fixnum; =A0=A0=A0 } =A0=A0=A0 if (rb_range_beg_len(argv[0], &beg, &len, RARRAY_LEN(ary), 1)) { =A0=A0=A0 =A0=A0=A0 /* check if idx is Range */ =A0=A0=A0 =A0=A0=A0 rb_ary_splice(ary, beg, len, argv[1]); =A0=A0=A0 =A0=A0=A0 return argv[1]; =A0=A0=A0 } =A0=A0=A0 offset =3D NUM2LONG(argv[0]); fixnum: =A0=A0=A0 rb_ary_store(ary, offset, argv[1]); =A0=A0=A0 VALUE s; =A0=A0=A0 int temp,i; =A0=A0=A0 for (i=3D0; i
=0A
Hi, I'm trying to get the value of a array from ruby source code,
That= is array.c

./ruby
arr
=3DArray.n= ew(3)
arr
[0]=3D5
arr
[1]=3D3
arr
[2]=3D2
=0A=0A

These= lines should call this function, https://github.com/ruby/ruby/blob/= trunk/array.c#L568 according to this, http://www.ruby-doc.org/core-1.9.= 3/Array.html


=0A=0A

So I have added couple of line= s there to display the values of array. But i didn't get the expected resul= t.

=0A=0A
el=
se {
    memfill
(RARRAY_PTR(ary), len= , val);
    ARY_SET_LEN
(ary, len);
   
int i;
&nbs= p;  
int result<= /span>;
    res= ult
=3D 0;
    VALUE
*s_arr =3D RARRAY_PTR(ary);
   
for(i =3D 0; i = < len; i++)<= /span> {
        result
= =3D LONG2NUM(<= span class=3D"pln">s_arr
[i]);        printf("r: %d\n",result);
&nbs= p;  
}
}
= =0A=0A

I got the result like this:

=0A=0A
arr=3DArray.new(3)
arr
= [0]=3D5
arr
[= 1]=3D3
arr
[<= /span>2]=3D2
r
:
9
r
: 9
r
: 9
<= /pre>=0A=0A

Why is this result? Why 9?

=0A=0A

I have followed these = to solve it:

=0A=0A=0A=0A


I also try editing/customizing= array.c:rb_ary_aset function like this:


static VALUE
r= b_ary_aset(int argc, VALUE *argv, VALUE ary)
{
    lon= g offset, beg, len;

    if (argc =3D=3D 3) {
 = ;       rb_ary_modify_check(ary);
  &= nbsp;     beg =3D NUM2LONG(argv[0]);
    &= nbsp;   len =3D NUM2LONG(argv[1]);
     &n= bsp;  rb_ary_splice(ary, beg, len, argv[2]);
    &nb= sp;   return argv[2];
    }
  &nbs= p; if (argc !=3D 2) {
        rb_raise(rb_= eArgError, "wrong number of arguments (%d for 2)", argc);
  &n= bsp; }
    rb_ary_modify_check(ary);
   = ; if (FIXNUM_P(argv[0])) {
        offset = =3D FIX2LONG(argv[0]);
        goto fixnum;    }
    if (rb_range_beg_len(argv[0], = &beg, &len, RARRAY_LEN(ary), 1)) {
     &nbs= p;  /* check if idx is Range */
      &nbs= p; rb_ary_splice(ary, beg, len, argv[1]);

     &= nbsp;  return argv[1];
    }

  &= nbsp; offset =3D NUM2LONG(argv[0]);
fixnum:
    rb_ary= _store(ary, offset, argv[1]);
    VALUE s;
  = ;  int temp,i;
    for (i=3D0; i<RARRAY_LEN(ary);= i++) {
        s =3D rb_inspect(RARRAY_PT= R(ary)[i]);
        switch (TYPE(s)) {
=             case T_FIXNUM:
=                 temp=3D0;
        &nbs= p;     break;
         = ;   case T_STRING:
         = ;     temp=3D1;
        &nb= sp;     break;
        &nbs= p;   case T_ARRAY:
         = ;     temp=3D2;
        &nb= sp;     break;
        &nbs= p;   case T_BIGNUM:
        &nbs= p;     temp=3D3;
        &n= bsp;     break;
        &nb= sp;   default:
         &nb= sp;    temp=3D4;
              rb_raise(rb_eTypeError, "not valid value");
&= nbsp;             break;
&n= bsp;         }
    }
&nb= sp;   return argv[1];



Still i can= 't understand how to get the value casting from VALUE var to int var.


Can anyone please help to display/printf the value of Ruby = array in C?


Someone please reply.


Any ans= wer would be highly appreciated.

Thanks in advance.



=0A=0A
---1624034222-1066378124-1340431702=:97157--