Changing value of ruby variables/references
reference, ruby, scope, variables
Solution
`my_var_a` and `my_var_set` are different references, but they point at the same object. If you modify the object in `my_var_set`, the change shows up in `my_var_a`. However, if you repoint `my_var_set` at a new object, that doesn't change what `my_var_a` points at.
Edit: clarification...
What Ruby does is called passing references by value. When you say
my_var_a = "nothing happend to me"
Ruby saves the string "nothing happend to me" in a memory location (let's call it 1000), and saves the `my_var_a` reference in another memory location (let's say 2000). When your code uses `my_var_a`, the interpreter looks at location 2000, see 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 reference named `my_var_set` and points it to the string that `my_var_a` was pointing at (memory location 1000). However, `my_var_set` is a copy of the `my_var_a` reference -- let's say `my_var_set` was created at memory location 3000. `my_var_a` and `my_var_set` are 2 completely different references in memory, they just happen to point at the same exact memory location which holds the string value.
The statement `my_var_set = "my value changed"` in `parse_set` creates a new string in memory and points `my_var_set` at that new memory location. However, this doesn't change what the original `my_var_a` reference points at! Now that `my_var_set` points at a different memory location, nothing that you do to that variable will affect `my_var_a`.
The same reference copy happens for `parse_sub` as well. But the reason that `parse_sub` changes the string is because you're calling a method directly on the `my_var_sub` reference. When you do this, the interpreter gets the object that `my_var_sub` is pointing at and then modifies it. So that change will show up in the `my_var_a` reference, because it still points at the same string.
Problem
I just stumbled upon something i don't quite understand. I know that variables in ruby are references. So that awesome stuff is possible. But when i pass a variable to a method, it behaves strangely: ``` my_var_a = "nothing happend to me" my_var_b = "nothing happend to me" def parse_set(my_var_set) my_var_set = "my value changed" end def parse_sub(my_var_sub) my_var_sub.sub! /(.*)/, "my value changed" end parse_set(my_var_a) parse_sub(my_var_b) my_var_a # => "nothing happend to me" my_var_b # => "my value changed" ``` Can you explain to me why it works with `sub!` and `=` leaves the object unchanged? How can I avoid to use `sub!` but having the same result?