Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The other answerers are all correct, but a friend asked me to explain this to him and what it really boils down to is how Ruby handles variables, so I thought I would share some simple pictures / explanations I wrote for him (apologies for the length and probably some oversimplification):</p> <hr> <h3>Q1: What happens when you assign a new variable <code>str</code> to a value of <code>'foo'</code>?</h3> <pre><code>str = 'foo' str.object_id # =&gt; 2000 </code></pre> <p><img src="https://i.stack.imgur.com/GzWSD.png" alt="enter image description here"></p> <h3>A: A label called <code>str</code> is created that points at the object <code>'foo'</code>, which for the state of this Ruby interpreter happens to be at memory location <code>2000</code>.</h3> <hr> <h3>Q2: What happens when you assign the existing variable <code>str</code> to a new object using <code>=</code>?</h3> <pre><code>str = 'bar'.tap{|b| puts "bar: #{b.object_id}"} # bar: 2002 str.object_id # =&gt; 2002 </code></pre> <p><img src="https://i.stack.imgur.com/5UABQ.png" alt="enter image description here"></p> <h3>A: The label <code>str</code> now points to a different object.</h3> <hr> <h3>Q3: What happens when you assign a new variable <code>=</code> to <code>str</code>?</h3> <pre><code>str2 = str str2.object_id # =&gt; 2002 </code></pre> <p><img src="https://i.stack.imgur.com/BVXvI.png" alt="enter image description here"></p> <h3>A: A new label called <code>str2</code> is created that points at the <em>same object</em> as <code>str</code>.</h3> <hr> <h3>Q4: What happens if the object referenced by <code>str</code> and <code>str2</code> gets changed?</h3> <pre><code>str2.replace 'baz' str2 # =&gt; 'baz' str # =&gt; 'baz' str.object_id # =&gt; 2002 str2.object_id # =&gt; 2002 </code></pre> <p><img src="https://i.stack.imgur.com/ZMFEd.png" alt="enter image description here"></p> <h3>A: Both labels still point at the same object, but that object itself has mutated (its contents have changed to be something else).</h3> <hr> <h3>How does this relate to the original question?</h3> <p>It's basically the same as what happens in Q3/Q4; the method gets its own private copy of the variable / label (<code>str2</code>) that gets passed in to it (<code>str</code>). It can't change which object the label <code>str</code> <em>points to</em>, but it can change the <em>contents of</em> the object that they both reference to contain else:</p> <pre><code>str = 'foo' def mutate(str2) puts "str2: #{str2.object_id}" str2.replace 'bar' str2 = 'baz' puts "str2: #{str2.object_id}" end str.object_id # =&gt; 2004 mutate(str) # str2: 2004, str2: 2006 str # =&gt; "bar" str.object_id # =&gt; 2004 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload