So is it just the shovel operator that modifies the original string? Why does this work, it looks like:
hi = original_string
is acting like some kind of a pointer? Can I get some insight as to when and how and why this behaves like this?
def test_the_shovel_operator_modifies_the_original_string
original_string = "Hello, "
hi = original_string
there = "World"
hi << there
assert_equal "Hello, World", original_string
# THINK ABOUT IT:
#
# Ruby programmers tend to favor the shovel operator (<<) over the
# plus equals operator (+=) when building up strings. Why?
end
In ruby, everything is a reference. If you do
foo = bar, nowfooandbarare two names for the same object.If, however, you do
foo = foo + bar(or, equivalently,foo += bar),foonow refers to a new object: one that is the result of the computationfoo + bar.