Clearing arrays etc. in extensions

Hi,

How do I clear the contents of an array or hash in
an extension? Setting the pointer to nil just doesn’t
seem to be right…

static VALUE t_reset (VALUE self)
{
VALUE arr;

arr = rb_iv_get(self, “@arr”);
RARRAY(arr)->ptr = ‘\0’; /* ??? */

return Qnil;
}

Thanks,

-mark.

What are you wanting to do? Make the array have 0 elements? You could use
rb_funcall to call the clear method on the array, or simply set the len
field to 0. Clearing a hash is less simple, so calling the clear method
seems best to me.

···

On Tue, 08 Oct 2002 05:54:30 +0900, Mark Probert wrote:

Hi,

How do I clear the contents of an array or hash in
an extension? Setting the pointer to nil just doesn’t
seem to be right…

Hi,

···

At Tue, 8 Oct 2002 07:32:59 +0900, Tim Hunter wrote:

or simply set the len
field to 0.

If you modify an object directly, it’s better to call
rb_check_frozen().


Nobu Nakada

Cheking whether frozen or not, in iteration or not, $SAFE permits
modifying the array or not. Then set len 0, and resize the underlying
bin if necessary.

That’s what done in rb_ary_clear(), implementing Array#clear and
is exported for C extension in intern.h :slight_smile:

I’ve look ruby-1.6.7 and -1.7.2. Perhaps more older version also have
the external declaration.

···

In message 200210080230.g982UiY10524@sharui.nakada.kanuma.tochigi.jp nobu.nokada@softhome.net writes:

or simply set the len
field to 0.

If you modify an object directly, it’s better to call
rb_check_frozen().


kjana@dm4lab.to October 8, 2002
Out of sight, out of mind.

Thank you so much! I knew that there was a simple answer
in there some where.

As a general question, using rb_ary_clear(arr) seems clearer
than rb_funcall(arr, rb_intern(“clear”), 0). Is there any
downside (besides the fact that it is “undocumented” and
buried in intern.h ;-)?

-mark.

···

At 08:53 PM 10/8/2002 +0900, kjana@dm4lab.to wrote:

That’s what done in rb_ary_clear(), implementing Array#clear and
is exported for C extension in intern.h :slight_smile:

Hello Mark,

Tuesday, October 08, 2002, 5:16:28 PM, you wrote:

···

At 08:53 PM 10/8/2002 +0900, kjana@dm4lab.to wrote:

That’s what done in rb_ary_clear(), implementing Array#clear and
is exported for C extension in intern.h :slight_smile:

Thank you so much! I knew that there was a simple answer
in there some where.

As a general question, using rb_ary_clear(arr) seems clearer
than rb_funcall(arr, rb_intern(“clear”), 0). Is there any
downside (besides the fact that it is “undocumented” and
buried in intern.h ;-)?

of course! :slight_smile: it can be overloaded, stolen or made private :slight_smile:

so you must decide what you want - just clear the array or order array to
clear itself :slight_smile:


Best regards,
Bulat mailto:bulatz@integ.ru

Hi,

···

At Tue, 8 Oct 2002 22:16:28 +0900, Mark Probert wrote:

As a general question, using rb_ary_clear(arr) seems clearer
than rb_funcall(arr, rb_intern(“clear”), 0). Is there any
downside (besides the fact that it is “undocumented” and
buried in intern.h ;-)?

Even if the arr is an instance of a subclass of Array which
overrides “clear” method, rb_ary_clear() always invokes the
original behavior. It may be expected behavior or not.


Nobu Nakada

Thanks for the info.

Regards,

-mark.

···

At 10:27 PM 10/8/2002 +0900, Nobu wrote:

At Tue, 8 Oct 2002 22:16:28 +0900, >Mark Probert wrote:

As a general question, using rb_ary_clear(arr) seems clearer
than rb_funcall(arr, rb_intern(“clear”), 0). Is there any
downside (besides the fact that it is “undocumented” and
buried in intern.h ;-)?

Even if the arr is an instance of a subclass of Array which
overrides “clear” method, rb_ary_clear() always invokes the
original behavior. It may be expected behavior or not.