Yeah, I figured something like gsub!(/(\A|\s)\w/) { $&.upcase } 
(using regular expression) would be better.

But I was experimenting and wanted something where I could see all 
the steps.  My attempt to produce the 'modify in place' 
functionality was to set "self = @rs" at the return point.  This 
produced the error:
./testSTR.rb:14: Can't change the value of self (SyntaxError)
                 self=@rs                     # return result string
                      ^
         from ./testSTR.rb:14
After seeing the code from Guy Decoux, I tried "self[0..-1] = @rs", 
which works fine.  So my question is why does:
      self[0..-1] = @rs
work?  While:
      self = @rs
does not.

Also, what exactly does dup.method do (suggested as a stupid way by 
Guy Decoux)?
~~LF


On Tuesday, January 29, 2002, at 09:47 AM, Matt Armstrong wrote:

> Larry Forrister <L4 / stetson.net> writes:
>
>> How do you create an in place modification method based on or 
>> equivalent to the
>> one listed below?  i.e.... proper_case!
>> ~~LF
>>
>> class String
>>     def proper_case!
>>         # ??????
>>     end
>>     def proper_case
>>         @pc=' '
>>         @rs=''
>>         self.each_byte{|@ib|
>>             @ic=@ib.chr
>>             if @pc==' '
>>                 @rs<<@ic.upcase
>>             else
>>                 @rs<<@ic
>>             end
>>             @pc=@ic
>>             }
>>         @rs
>>     end
>> end
>
> You can replace one string value with another with the String#replace
> method.
>
>     class String
>       def proper_case!
>         proper = self.proper_case
>         if self != proper
>           replace(proper)
>         else
>           nil
>         end
>       end
>     end
>
> However, I think your methods are better implemented with gsub and
> gsub!
>
>     class String
>       def proper_case!
>         gsub!(/(\A|\s)\w/) { $&.upcase }
>       end
>       def proper_case
>         gsub(/(\A|\s)\w/) { $&.upcase }
>       end
>     end
>
>
> --
> matt