Why is character in string not mutated?
Why is character in string not mutated? def test_problem(str) str[3].upcase! # str[3] = str[3].upcase! works str end p test_problem("hello") My question is why String.upcase! which is mutating method doesn't mutate string in a case above but you need to reassign that character in string? 2 Answers 2 String# returns a new string, as is documented. String# a = "foo" a.object_id # => 70217975553640 a[0].object_id # => 70217957574840 A string is not composed of character objects, it is a single object (at least on a superficial level, I'm not sure of the C internals). So there is no way to extract a character and still have it belong to the original string - you need to work with the string as a whole if you want to mutate it. String#= on the other hand does mutate the string String#= You could make your method like so: def test_problem(str) str[3] = str[3].upca...
