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…
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
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
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
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! it can be overloaded, stolen or made private
so you must decide what you want - just clear the array or order array to
clear itself
–
Best regards,
Bulat mailto:bulatz@integ.ru