my_var_a and my_var_set are different links, but they point to the same object. If you change the object in my_var_set , the change will appear in my_var_a . However, if you my_var_set on a new object, this does not change what my_var_a points to.
Edit: Explanation ...
What Ruby does is refer to passing values by value. When you say
my_var_a = "nothing happend to me"
Ruby saves the line “nothing happened to me” in the memory cell (let it be called 1000) and saves the link my_var_a in another memory cell (say 2000). When your code uses my_var_a , the interpreter looks at location 2000, sees that it points to 1000, then gets the actual string value from 1000.
When you call parse_set(my_var_a) , Ruby actually creates a new link called my_var_set and points it to the line pointed to by my_var_a (memory 1000). However, my_var_set is a copy of the my_var_a link — let's say my_var_set was created in memory location 3000. my_var_a and my_var_set are two completely different links in memory, they just point to the exact exact memory location that the string value contains.
The my_var_set = "my value changed" parse_set in parse_set creates a new line in memory and points my_var_set in this new memory location. However, this does not change what the reference point my_var_a ! Now, when my_var_set points to another memory location, nothing you do with this variable will affect my_var_a .
The same copying of links occurs for parse_sub . But the reason parse_sub modifies the string is because you are calling the method directly from my_var_sub . When you do this, the interpreter gets the object that my_var_sub points my_var_sub , and then modifies it. Thus, the change will be displayed in the my_var_a directory, since it still points to the same line.
source share