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?
I've had to use
replace
exactly zero times in all the years I've been programming in Ruby. I wonder if there's a better design for your code.Most string methods which change the receiver are suffixed by ! (sub!, capitalize!, chomp!, etc.) Some that change the receiver are not suffixed by ! (replace is one). If you call a method that changes the receiver, any and all references to that object will see the change. if you call a method that does not change receiver, the method returns a new string.
gsub does not modify the receiver, but instead returns a new instance of String:
gsub! does modify the receiver:
"How can I avoid to use sub! but having the same result?"
Set it as an instance variable of an object (although this works just in your main script, I recommend making your own class if you want to use the instance variables approach).
my_var_a
andmy_var_set
are different references, but they point at the same object. If you modify the object inmy_var_set
, the change shows up inmy_var_a
. However, if you repointmy_var_set
at a new object, that doesn't change whatmy_var_a
points at.Edit: clarification...
What Ruby does is called passing references by value. When you say
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 usesmy_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 namedmy_var_set
and points it to the string thatmy_var_a
was pointing at (memory location 1000). However,my_var_set
is a copy of themy_var_a
reference -- let's saymy_var_set
was created at memory location 3000.my_var_a
andmy_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"
inparse_set
creates a new string in memory and pointsmy_var_set
at that new memory location. However, this doesn't change what the originalmy_var_a
reference points at! Now thatmy_var_set
points at a different memory location, nothing that you do to that variable will affectmy_var_a
.The same reference copy happens for
parse_sub
as well. But the reason thatparse_sub
changes the string is because you're calling a method directly on themy_var_sub
reference. When you do this, the interpreter gets the object thatmy_var_sub
is pointing at and then modifies it. So that change will show up in themy_var_a
reference, because it still points at the same string.