3 ms·
Isn't the string type immutable in many (most?) other languages as well? In Objective-C the default is an immutable string (though optionally one can create mu
by wsc981 13y ago
Isn't the string type immutable in many (most?) other languages as well? In Objective-C the default is an immutable string (though optionally
one can create mutable strings as well). Lua also uses immutable strings. In Java and C# I think the situation is the same, since if you want
to use high performance string manipulation, you'll generally resort some form of StringBuilder helper class.
- meepmorp 13y agoI believe strings are mutable in Ruby.
- reflectiv 13y agoThey are... s = "hello" s << " world" s # hello world
- dbaupp 13y agoIs that allocating a new buffer, leaving the "hello" string to be collected by the GC?
- tedunangst 13y agoNo, it expands the existing buffer. (leaving " world" to be collected). Note that the following is different and more like what you're thinking. a = a + " world"
- jeorgun 13y agoNo, it's operating in place: def append_world(str) str << " world" end a = "hello" append_world(a) a #=> "hello world"
- SideburnsOfDoom 13y agoCorrect, C# and .Net have an immutable string class and a mutable StringBuilder helper class.