I’ve wrote two ruby files for test
test.rb:
#!/usr/bin/ruby
def foo(bar)
bar['key'] = 'value'
end
def my_print(a)
a.each{|k,v|
puts "#{k} => #{v}"
}
end
test_drive.rb:
#!/usr/bin/ruby
require 'test.rb'
hash_test = Hash.new
foo(hash_test)
my_print(hash_test)
It works as what I expect, the output is
key => value
But when I chanege the test.rb to
#!/usr/bin/ruby
def foo(bar)
pre_defined = {'key' => 'value'}
bar = pre_defined
end
def my_print(a)
a.each{|k,v|
puts "#{k} => #{v}"
}
end
Here I used a pre-defined hash, but now it outputs nothing. The “hash_test” is now an empty hash.
Please illustrate me why indeed does this happen?
Here is the simple answer:
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.
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.
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:
I hope this helps. In case you need something a little more detailed:
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.