I'm working through a tutorial right now, and I'd like to understand why the following occurs:
original_string = "Hello, "
hi = original_string
there = "World"
hi += there
assert_equal "Hello, ", original_string
original_string = "Hello, "
hi = original_string
there = "World"
hi << there
assert_equal "Hello, World", original_string
Why does +=
have no effect on original_string
, and <<
does? I was absolutely certain that the second case would also equal "Hello, "
, but that's not the case.
hi = original string
in the first example appears to copy the value of original_string
into hi
, but hi = original string
in the second example appears to set hi
to point to the same string as original string
. I would guess there is some kind of implicit decision behind the scenes as to whether to copy the value or copy the reference... or something.