Question about Array#*

Discussion in 'Ruby' started by Magicloud Magiclouds, Mar 24, 2008.

  1. 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.
     
    Magicloud Magiclouds, Mar 24, 2008
    #1
    1. Advertising

  2. On Monday 24 March 2008, Magicloud Magiclouds 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
     
    Stefano Crocco, Mar 24, 2008
    #2
    1. Advertising

  3. 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 Crocco wrote:
    > On Monday 24 March 2008, Magicloud Magiclouds 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
    >
    >
    >
    >
     
    Magicloud Magiclouds, Mar 24, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joseph Turian
    Replies:
    11
    Views:
    522
    Joseph Turian
    Jan 19, 2005
  2. Fabio
    Replies:
    0
    Views:
    313
    Fabio
    Nov 5, 2008
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    496
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    213
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    478
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page