-
Is the array index returned or is the whole array returned?
-
And what about the life of the array? If we don’t return the created array, does it continue to take memory space or does it free up automatically when we leave the function?
I am new to OOP and Ruby. I’ve done C previously.
A couple of things about Ruby. First, Ruby is a garbage collected language, so depending on the implementation of Ruby, the garbage collector will free up the memory space at different times.
Second, it’s unclear what you mean by an “array return”. If you mean from a method, then the question of what happens under the hood is probably also one of implementation. From the point of view of the programmer, an entire array is returned. Whether the same block of memory is being referenced is most likely a question about the scope of the variable, it is certainly possible to return an entire array by value rather than by reference.
The “standard” implementation of Ruby, also known as MRI or Matz’s Ruby, is written in C and open sourced, so you can see the C code for an array, including it’s memory allocation and deallocation functions.