From: Gavin Sinclair Date: 2004-09-09T09:47:14+09:00 Subject: Re: Q: Should String.upcase! work on a segment of a string? Mike Hall wrote: > 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 is one object; s[4..5] is another. What you do to the second object is none of the first's business. > > s[4..5] = s[4..5].upcase # so, do it by-hand > => "ZZ" > > > s # and it's ok > => "AA11ZZ" That's because of (psuedo-code) class String def []=(index, value) case index when Range then ... when Numeric then ... ... end end end > Are there edge cases where this wouldn't be appropriate? There's no way to make s[4..5].upcase! do what you suggest. Cheers, Gavin