# Dissapearing data from array

**URL:** <https://rubytalk.org/t/dissapearing-data-from-array/58057>\
**Category:** ruby-talk\
**Created:** [7 April 2010 17:47 UTC](https://rubytalk.org/t/dissapearing-data-from-array/58057 "2010-04-07T17:47:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cassna\_Capriet](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@Cassna\_Capriet](https://rubytalk.org/u/Cassna_Capriet)\
**Post date:** [7 April 2010 17:47 UTC](https://rubytalk.org/t/dissapearing-data-from-array/58057/1 "2010-04-07T17:47:37Z")

</div>

Hi all.

I have the next problem.

a = %w(a b)  
z = %w(1)  
a.insert(1, z)  
puts a  
&nbsp;&nbsp;a  
&nbsp;&nbsp;1  
&nbsp;&nbsp;b

z.clear  
puts a  
&nbsp;&nbsp;a  
&nbsp;&nbsp;b

Oopss... where "1" is? what happened? what can I do to keep "1" when I  
clear the array (z)?

Thanks a lot.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Florian\_Gilcher](https://avatars.discourse-cdn.com/v4/letter/f/57b2e6/32.png) [@Florian\_Gilcher](https://rubytalk.org/u/Florian_Gilcher)\
**Post date:** [7 April 2010 18:01 UTC](https://rubytalk.org/t/dissapearing-data-from-array/58057/2 "2010-04-07T18:01:41Z")

</div>

> Hi all.
> 
> I have the next problem.
> 
> a = %w(a b)  
> z = %w(1)  
> a.insert(1, z)  
> puts a  
> a  
> 1  
> b

Try

&nbsp;&nbsp;puts a.inspect  
&nbsp;&nbsp;#=\> ["a", ["1"], "b"]

> z.clear  
> puts a  
> a  
> b

&nbsp;&nbsp;puts a.inspect  
&nbsp;&nbsp;#=\> ["a", , "b"]

> Oopss... where "1" is? what happened? what can I do to keep "1" when I  
> clear the array (z)?

Ruby uses references. So a.insert(1, z) inserts a reference to z at position 1 in the array. So once you clear z, it will be cleared noticed by everyone referring to it - a, in this case.

Regards,  
Florian

> **···**
>
> On Apr 7, 2010, at 7:47 PM, Noé Alejandro wrote:

---

<div class="post-metadata">

**Author:** ![Andrea\_Dallera](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@Andrea\_Dallera](https://rubytalk.org/u/Andrea_Dallera)\
**Post date:** [7 April 2010 18:02 UTC](https://rubytalk.org/t/dissapearing-data-from-array/58057/3 "2010-04-07T18:02:38Z")

</div>

> From what i've understood your aim is to "detach" the reference of the

inserted element from the original one. That can be done with z.clone,  
in your example it will look like this:

a = %w(a b)  
z = %w(1)  
a.insert(1, z.clone)  
z.clear  
puts a  
&nbsp;&nbsp;a  
&nbsp;&nbsp;1  
&nbsp;&nbsp;b

> **···**
>
> --  
> Andrea Dallera
> 
> > **[GitHub - bolthar/freightrain: Ruby desktop development made easy](https://github.com/bolthar/freightrain)**
> >
> > Ruby desktop development made easy. Contribute to bolthar/freightrain development by creating an account on GitHub.
> 
> > **[using (IMHO)](https://usingimho.wordpress.com/)**
> >
> > Andrea Dallera 's blog
> 
> On Thu, 2010-04-08 at 02:47 +0900, Noé Alejandro wrote:
> 
> > a.insert(1, z)  
> > puts a

---

<div class="post-metadata">

**Author:** ![Cassna\_Capriet](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@Cassna\_Capriet](https://rubytalk.org/u/Cassna_Capriet)\
**Post date:** [7 April 2010 18:33 UTC](https://rubytalk.org/t/dissapearing-data-from-array/58057/4 "2010-04-07T18:33:52Z")

</div>

Thanks guys... Florian for your explanation, Andrea for your solution.  
Now I understand what happens and how to solve it.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
