How do arrays in Ruby work?

P

Peter Wu

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?
 
L

Luc Traonmilin

[Note: parts of this message were removed to make it a legal post.]

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.

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?
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

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

arr.slice(1)
or
arr[1]

arr.slice[1] won't work since slice requires arguments, and this is being
interpreted as
(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"


--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
P

Peter Wu

Rick said:
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.
 
M

Miguel Regedor

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?

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
 

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

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top