Issue #14251 has been updated by ricardovsilva (Ricardo Silva).


This still very confusing to me and I believe that it can leads so many developers to a pitfall.
Because if you do
~~~ ruby
foo = 'aaa'
bar = foo
bar = bar + 'ccc' #here references of foo doesn't changes

puts foo #aaa
pubs bar #aaaccc
~~~

As I can see, ruby only for string as reference when I call string methods, like:

~~~ ruby
foo = 'abc'
bar = foo
bar.reverse!

puts foo #cba
puts bar #cba
~~~

Is that correct?

----------------------------------------
Bug #14251: String insert changing value of other string
https://bugs.ruby-lang.org/issues/14251#change-69066

* Author: ricardovsilva (Ricardo Silva)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
~~~ ruby
foo = 'abc'
bar = foo
bar.insert(1, d)

puts foo 'adbc'
puts bar 'adbc'
~~~

The example above should only affect bar variable. It leads to an error by programmer.

A bypass that I found is to do:

~~~ ruby
foo = 'aaa'
bar = String.new foo
bar.insert(1, 'd')
puts foo #aaa
puts bar #adaa
~~~





-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>