Are strings mutable in Ruby?
问题 Are Strings mutable in Ruby? According to the documentation doing str = "hello" str = str + " world" creates a new string object with the value "hello world" but when we do str = "hello" str << " world" It does not mention that it creates a new object, so does it mutate the str object, which will now have the value "hello world" ? 回答1: Yes, << mutates the same object, and + creates a new one. Demonstration: irb(main):011:0> str = "hello" => "hello" irb(main):012:0> str.object_id => 22269036