# How do arrays in Ruby work?

**URL:** <https://rubytalk.org/t/how-do-arrays-in-ruby-work/52286>\
**Category:** ruby-talk\
**Created:** [14 March 2009 14:12 UTC](https://rubytalk.org/t/how-do-arrays-in-ruby-work/52286 "2009-03-14T14:12:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Wu1](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@Peter\_Wu1](https://rubytalk.org/u/Peter_Wu1)\
**Post date:** [14 March 2009 14:12 UTC](https://rubytalk.org/t/how-do-arrays-in-ruby-work/52286/1 "2009-03-14T14:12:41Z")

</div>

As in how they are organized behind the scenes.

For example, if I say

arr = [1, 2, 3, 4]  
arr.slice[1]

Is it going to have to physically relocate everything after 2 in memory?  
Would using a linked list be more efficient if a lot of elements are  
going to be removed and inserted?

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

---

<div class="post-metadata">

**Author:** ![Luc](https://avatars.discourse-cdn.com/v4/letter/l/9dc877/32.png) [@Luc](https://rubytalk.org/u/Luc)\
**Post date:** [14 March 2009 14:38 UTC](https://rubytalk.org/t/how-do-arrays-in-ruby-work/52286/2 "2009-03-14T14:38:21Z")

</div>

slice only returns the value within the range or at index but does not  
modify the original array. slice![1] would effectively remove value at index  
1 from arr.  
I don't know how it is implemented.

> **···**
>
> On Sat, Mar 14, 2009 at 3:12 PM, Peter Wu \<pwu.380@gmail.com\> wrote:
> 
> > As in how they are organized behind the scenes.
> > 
> > For example, if I say
> > 
> > arr = [1, 2, 3, 4]  
> > arr.slice[1]
> > 
> > Is it going to have to physically relocate everything after 2 in memory?  
> > Would using a linked list be more efficient if a lot of elements are  
> > going to be removed and inserted?  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> Luc Traonmilin

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [14 March 2009 14:44 UTC](https://rubytalk.org/t/how-do-arrays-in-ruby-work/52286/3 "2009-03-14T14:44:11Z")

</div>

> As in how they are organized behind the scenes.
> 
> For example, if I say
> 
> arr = [1, 2, 3, 4]  
> arr.slice[1]

I think you meant either

&nbsp;&nbsp;arr.slice(1)  
or  
&nbsp;&nbsp;arr[1]

arr.slice[1] won't work since slice requires arguments, and this is being  
interpreted as  
&nbsp;&nbsp;(arr.slice)[1]

> Is it going to have to physically relocate everything after 2 in memory?  
> Would using a linked list be more efficient if a lot of elements are  
> going to be removed and inserted?

So far we haven't done anything which would add or remove anything

arr.slice(1) simply returns a reference to the object in the second element  
of arr. arr is untouched

arr.slice(1,3) would return an array containing the second, third and fourth  
elements, the original array would be untouched. AND

The MRI ruby implementation of arrays uses a copy on write scheme, so that  
both arr and the returned object would actually share memory for the array  
of references. If one or the other changed then a copy would be triggered.

Ruby arrays are implemented rather efficiently both in terms of space and  
speed. It's usually best to use them simply and only worry about performance  
issues when they arise, and when they do, it will probably be something  
other than "I should have used a linked list instead of an array"

> **···**
>
> On Sat, Mar 14, 2009 at 10:12 AM, Peter Wu \<pwu.380@gmail.com\> wrote:
> 
> --  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![Miguel\_Regedor](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@Miguel\_Regedor](https://rubytalk.org/u/Miguel_Regedor)\
**Post date:** [15 March 2009 12:32 UTC](https://rubytalk.org/t/how-do-arrays-in-ruby-work/52286/4 "2009-03-15T12:32:43Z")

</div>

if you have the array a = [1,2,3,4,5]

b = a.slice!(0,3)  
will end up with a == [4,5] and b == [1,2,3]

b = a.slice(0,3)  
will end up with a == [1,2,3,4,5] and b == [1,2,3]

for the last one you can also use b = a[0..2] , you will get the same  
result

> **···**
>
> On Mar 14, 2:12 pm, Peter Wu \<pwu....@gmail.com\> wrote:
> 
> > As in how they are organized behind the scenes.
> > 
> > For example, if I say
> > 
> > arr = [1, 2, 3, 4]  
> > arr.slice[1]
> > 
> > Is it going to have to physically relocate everything after 2 in memory?  
> > Would using a linked list be more efficient if a lot of elements are  
> > going to be removed and inserted?  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Peter\_Wu1](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@Peter\_Wu1](https://rubytalk.org/u/Peter_Wu1)\
**Post date:** [14 March 2009 16:55 UTC](https://rubytalk.org/t/how-do-arrays-in-ruby-work/52286/5 "2009-03-14T16:55:02Z")

</div>

Rick Denatale wrote:

> So far we haven't done anything which would add or remove anything
> 
> arr.slice(1) simply returns a reference to the object in the second  
> element  
> of arr. arr is untouched
> 
> arr.slice(1,3) would return an array containing the second, third and  
> fourth  
> elements, the original array would be untouched.

That's my mistake, arr.slice!(1) was the intention. But I see what  
you're saying.

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