Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the simple answer:</p> <p>In ruby you pass variables with Objects by reference. When you assign an Object to a variable, the variable does not really contain the Object itself within it. Instead, it contains only a reference to that Object.</p> <p>It may help for you to start seeing the differences between references and objects in order to understand how sending variables as parameters works: the Object itself does not reside in the variable, the variable is pointing to a reference of the Object in memory, because of this, if one variable points to another object and modifies it, that doesn't mean it modified the Object that it was previously referring to.</p> <p>The important thing for you to understand is that the bar parameter in the foo method is really only a REFERENCE to the Object in memory, not the Object itself. So if bar once pointed to the Object, but it's now referencing another Object, it will modify what it is referencing, not the previous object it pointed to. Here's the code of the last test.rb commented slightly for you to understand it better:</p> <pre><code>def foo(bar) # here "bar" points to the same object as "hash_test" pre_defined = {'key' =&gt; 'value'} # here a new object is created and referenced bar = pre_defined # now "bar" points to a new object and forgets about "hash_test" end def my_print(a) # here "a" holds a reference to "hash_test" which is empty a.each{|k,v| # "a" is empty, so it has nothing to put puts "#{k} =&gt; #{v}" } end </code></pre> <p>I hope this helps. In case you need something a little more detailed:</p> <p>The reason your last version of test.rb prints an empty hash in test_drive.rb is because the Hash Object that the reference "hash_test" points to is not really being modified at all. Instead the foo method, while initially receiving as a parameter called "bar" a reference of the Hash Object that "hash_test" points to, quickly replaces that reference in "bar" with a brand new reference to a brand new Hash Object that the "pre_defined" variable points to. Now "bar" doesn't point to the same Object as "hash_test" does, and so the Object that "hash_test" points to is never being modified. Thus, the Hash Object that "hash_test" contains is never really populated with anything, and is empty when my_print tries to print it.</p>
    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. 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