From: "Mehr, Assaph (Assaph)" Date: 2004-09-09T09:42:45+09:00 Subject: Re: Should String.upcase! work on a segment of a string? >Question: Should String.upcase! work on a segment of a string? > >We can assign to a sub-string, so I thought that a bang-method might also work on a sub-string. > > s = 'AA11zz' # define a string > > > s[4..5].upcase! # try to change part of it > => "ZZ" # yeah, ok, the new string is changed, but... > > > s > => "AA11zz" # the original string wasn't affected > > > s[4..5] = s[4..5].upcase # so, do it by-hand > => "ZZ" > > > s # and it's ok > => "AA11ZZ" > >Are there edge cases where this wouldn't be appropriate? The String#[] method creates a new string object. Any method you send to it (like upcase) will operatate on it and not on the original object. Even if we disregard that a lot of existing code relies on it, how would you differentiate between: s[4..5].upcase # should that return 'AA11ZZ' as a new object? s[4..5].upcase! # to match what is returned here? How could you then get just the 'ZZ' part?