This is consistent with the design of Ruby, as you have noticed. Immutable strings are more efficient than mutable strings β less copying because strings are reused β but making the job harder for the programmer. Intuitively see strings as mutable - you can combine them together. To deal with this, Java silently translates the concatenation (via + ) of two strings into a StringBuffer object, and I'm sure there are other similar hacks. Instead, Ruby chooses that strings change by default due to performance.
Ruby also has a number of destructive methods, such as String#upcase! that rely on mutable strings.
Another possible reason is that Ruby is inspired by Perl and Perl uses mutable strings.
Ruby has characters and frozen strings, both of which are immutable. As an added bonus, characters are guaranteed to be unique for every possible string value.
rjh Apr 09 '10 at 15:03 2010-04-09 15:03
source share