What memory is created/referenced when when an object is passed to a method? [duplicate]

0 votes
asked Sep 7, 2012 by rudolph9

Possible Duplicate:
Is Ruby pass by reference or by value?

Working with Ruby, when passing an object to a method, how is the memory of this object handled?

Coming from a C background, I can think of several things which may be happening:

  1. A copy of the memory associated with the according object and is made available to the method being called. In which case the modification of the object would only be reflected in the context of method being called, and not the calling method.

  2. A reference to the memory of the object is passed the method being called (essentially a pointer). Hence any changes made by the object by the method being called or the calling method would be reflected in both contexts. As well, should this program be multithreaded, some kind of mechanism (mutex, semaphore, etc.) must be used to ensure mutually exclusive access to that memory performing write operations.

  3. Something else I am unable to think of... maybe a memory model similar to that of Go... Pipes... MessagePassing...?

What is actually happening?

1 Answer

0 votes
answered Dec 23, 2018 by nucc

Ruby uses your second option, passes the parameter by reference.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...