Question about Array#*

Hello,
In ri 'Array#*', it says that the result is the concatenation of
copies of self if the argument is an int.
So, it is “COPY”.
Then why something like ‘[[0]] * 10’, returns an array, that each
element is a pointer to the first element?
Does this mean “copy of an Array” is just a duplicated pointer?

Thanks.

On Monday 24 March 2008, Magicloud M. wrote:

Hello,
In ri 'Array#*', it says that the result is the concatenation of
copies of self if the argument is an int.
So, it is “COPY”.
Then why something like ‘[[0]] * 10’, returns an array, that each
element is a pointer to the first element?
Does this mean “copy of an Array” is just a duplicated pointer?

Thanks.

I think that here the word “copy” has the same meaning it has in clone
and
dup, that is it means a shallow copy: the array itself is copied, but
its
contents aren’t. In other words, what you get using Array#* with a
number is
an array of size n times the original one, whose contents are the
contents of
the original array repeated n times.

I hope this helps

Stefano

In source, I got this: MEMCPY(RARRAY(ary2)->ptr+i, RARRAY(ary)->ptr,
VALUE, RARRAY(ary)->len);
So, I just got a bunch of pointer to the same object…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs