Reputation: 506
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?
Upvotes: 1
Views: 1413
Reputation: 495
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:
def foo(bar) # here "bar" points to the same object as "hash_test"
pre_defined = {'key' => '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} => #{v}"
}
end
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.
Upvotes: 3
Reputation: 2063
That is, because Ruby is pass by value, whereby the reference of an object is passed. It’s a similar behavior of what Java does. That means that you have only a copy of the reference within the foo-method and reassigning it does not change the reference outside of this method.
In more detail: In your first example you pass the Hash to your foo-method. The reference will be copied, so within the foo-method you have a copy of the reference which points to the same object as 'hash_test', but is not the same one. So calling the Hash methods changes the values of the object outside of the method. But in your second example your are not changing values of the given Hash, you assign a new Hash object to the copy of the method reference, which has no effect to the 'hash_test' reference.
Upvotes: 1