Brian Tully wrote: > on 6/22/04 3:36 PM, Joel VanderWerf at vjoel / PATH.Berkeley.EDU wrote: > > >>Brian Tully wrote: >> >> >>>Should I use state.squeeze(" ") to ensure that it always returns the state >>>even if nothing was changed? squeeze! seems to return nil if nothing needs >>>to be changed. >> >>str.squeeze! is destructive (changes the String object referred to by >>str), and str.squeeze is not. So it just depends on whether you need to >>keep the original string intact (I'd guess not). > > > > Hmmm I'm a little hazy... > > I want Ruby to strip out any instances of multiple whitespace and return the > "correct" string. I also want it to return the string regardless of whether > the squeeze function modified it. > > So in these examples I would hope for the following: > > @state = @user['state'].squeeze(" ") > > if @user['state'] was originally "New York" I would hope that the above > would set @state to "New York". > > In addition if @user['state'] was originally "California" I would hope that > the above would still set @state to "California". This is one way: @state = @user['state'] @state.squeeze!(" ") After this code, the string referenced by both @state and @user['state'] will be have no successive pairs of whitespace.