Question about Array#*

  • Thread starter Magicloud Magiclouds
  • Start date
M

Magicloud Magiclouds

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.
 
S

Stefano Crocco

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
 
M

Magicloud Magiclouds

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....


Stefano said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top