Arrays in Ruby: Benefits and Costs to Use Them

Arrays in Ruby: Benefits and Costs to Use Them hackernoon.com2 years ago in #Dev Love34

@Figle71Facundo Iglesias Front end web developer student at Microverse Data structures are eagerly introduced to new programmers, since they will be using them pretty much through their whole career. Knowing them, how they work, what are they good for, which one to use, are things that can change the performance of your solutions quite vastly. On this article, as the title suggest, I will be talking arrays with ruby, and going into a bit of detail here and there about how this language manages them. Article is intended to beginners with a general understanding of basic concepts. Some ruby array knowledge is necessary to follow some examples, such as adding elements, concatenation, and looping. Arrays. What, how and where. Arrays are, as the word semantic suggest, a consecutive collection of objects. This definition is quite literal in most languages, meaning that objects stored in said structure would be placed sequentially in memory as well as in the abstraction that the developer will be using at a high level. Usually, we would find two different ‘types’ of Arrays. Static, and Dynamic. Static arrays are those which we define the size, and that is set in stone. Dynamic arrays, in the other hand, grow or shrink, typically used when we do not know the final size of this structure. Check this next example of an implementation (using C not ruby, but bear with me) #include int main() { int intArray [10]; printf(“Size of int: %ld bytesn”, sizeof(int)); for (int i=0; i < 10; i ) { printf("nElementt%p",&intArray[i]); } return 0; } Now, this trivial piece of code, when executed will output a very literal representation of what an array in a programming a computer usually is. Size of int: 4 bytes Element 0x7ffe9a634b50 Element 0x7ffe9a634b54 Element 0x7ffe9a634b58 Element 0x7ffe9a634b5c Element 0x7ffe9a634b60 Element 0x7ffe9a634b64 Element 0x7ffe9a634b68 Element 0x7ffe9a634b6c Element 0x7ffe9a634b70 Element 0x7ffe9a634b74 We defined a static array of integers. More specifically, a static array that will hold up to 10 integers. We can see that every integer, will have up to 4 bytes of data and even thought our array is still empty, memory is already reserved an ready to use. Not only that, but their address are separated by these 4 bytes in particular, which gives you an idea of how it could work. You have the first memory cell address, if you go 4 bytes from there, you will find the second element. So, we can assume that something like this : could work by starting on the first direction and adding 4*[size of object] to that address, to get to that direction and get the value in it. Why is it important that memory is already reserved ? Basically, because the first concept of “everything is next to each other in memory“. Therefore, you first need to find a place in memory with [size] consecutive free spaces before start placing them. “But wait, I always use .push(element) in Ruby and never had an issue with reserving memory beforehand “. That is correct! And that is because you were using a dynamic array. Dynamic Arrays A dynamic array is a particular structure that, although still being an array, is a bit more complex and has some more functionality added to make your life easier by making the everyday array feel extremely flexible.  » Read More

Like to keep reading?

This article first appeared on If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply