TL;DR
In your original question, now edited, you are confusing recursion with mutation and propagation. All three concepts are useful tools in the right situations, and when the behavior is expected. You likely find the particular example you posted confusing because you aren't expecting the string to mutate in place, or for the change to propagate across all pointers to that object.
The ability to generalize methods is what enables duck-typing in dynamic languages like Ruby. The main conceptual hurdle is understanding that variables point to objects, and only experience with the core and standard libraries will enable you to understand how objects respond to particular messages.
Strings in Ruby are full-fledged objects that respond to messages, rather than simply being language primitives. In the following sections, I attempt to explain why this is rarely a problem, and why the feature is useful in a dynamic language like Ruby. I also cover a related method that produces the behavior you were originally expecting.
It's All About Object Assignment
My question is why is this a thing. I understand that setting "b=a" makes them them the same object_id so there technically two names for the same variable string.
This is rarely a problem in everyday programming. Consider the following:
a = 'foo' # assign string to a
b = a # b now points to the same object as a
b = 'bar' # assign a different string object to to b
[a, b]
#=> ["foo", "bar"]
This works the way you'd expect, because the variable is just a placeholder for an object. As long as you're assigning objects to variables, Ruby does what you might intuitively expect.
Objects Receive Messages
In your posted example, you're running into this behavior because what you're really doing is:
a = 'foo' # assign a string to a
b = a # assign the object held in a to b as well
b.replace 'bar' # send the :replace message to the string object
In this case, String#replace is sending a message to the same object pointed to by both a and b. Since both variables hold the same object, the string is replaced whether you invoke the method as a.replace
or b.replace
.
This is perhaps not intuitive, but it is rarely a problem in practice. In many cases, this behavior is actually desirable so that you can pass objects around without caring how a method labels an object internally. This is useful for generalizing a method, or for self-documenting a method's signature. For example:
def replace_house str
str.sub! 'house', 'guard'
end
def replace_cat str
str.sub! 'cat', 'dog'
end
critter = 'house cat'
replace_house critter; replace_cat critter
#=> "guard dog"
In this example, each method expects a String object. It doesn't care that the string is labeled critter elsewhere; internally, the method uses the label str to refer to that same object.
As long as you know when a method mutates the receiver and when it passes back a new object, you will be unsurprised by the results. More on this in a moment.
What String#replace Really Does
In your specific example, I can see how the documentation for String#replace might be confusing. The documentation says:
replace(other_str) → str
Replaces the contents and taintedness of str with the corresponding values in other_str.
What this really means is that b.replace
is actually mutating the object ("replacing the contents"), not returning a new object for assignment to the variable. For example:
# Assign the same String object to a pair of variables.
a = 'foo'; b = a;
a.object_id
#=> 70281327639900
b.object_id
#=> 70281327639900
b.replace 'bar'
#=> "bar"
b.object_id
#=> 70281327639900
a.object_id == b.object_id
#=> true
Note that the object_id never changes. The particular method you used reuses the same object; it just changes its contents. Contrast this with methods like String#sub which return a copy of the object, which means you'd get back a new object with a different object_id.
What to Do Instead: Assigning New Objects
If you want a and b to point to different objects, you can use a non-mutating method like String#sub instead:
a = 'foo'; b = a;
b = b.sub 'oo', 'um'
#=> "fum"
[a.object_id, b.object_id]
#=> [70189329491000, 70189329442400]
[a, b]
#=> ["foo", "fum"]
In this rather contrived example, b.sub
returns a new String object, which is then assigned to the variable b. This results in different objects being assigned to each variable, which is the behavior you were originally expecting.