From: Austin Ziegler Date: 2004-09-10T04:52:57+09:00 Subject: Re: Q: Should String.upcase! work on a segment of a string? On Fri, 10 Sep 2004 04:40:02 +0900, Mike Hall wrote: > Gavin Sinclair wrote: > >> 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. > > I don't understand that... we can outright assign to the substring and it _does_ affect the > original string. > > I agree that pulling out s[4..5] (using it on the RHS) will give me a new object, that's cool. There's your conceptual error: there is no distinction of RHS and LHS. There's a method, String#[]=, that is being used on the LHS when you do: s[4..5] = s[4..5].upcase So that's actually: s.__send__(:"[]="), 4..5, s[4..5].upcase) String#[] returns a new object; String#[]= performs an indexed assignment that is interpreted in String as replacing 4..5 with the value provided. -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca : as of this email, I have [ 3 ] Gmail invitations