<分区>
考虑以下代码:
$ irb
> s = "asd"
> s.object_id # prints 2171223360
> s[0] = ?z # s is now "zsd"
> s.object_id # prints 2171223360 (same as before)
> s += "hello" # s is now "zsdhello"
> s.object_id # prints 2171224560 (now it's different)
似乎可以在不创建新字符串的情况下更改单个字符。然而,附加到字符串显然会创建一个新字符串。
Ruby 中的字符串是可变的吗?