On 6/5/07, Mike <michaelst / gmail.com> wrote:
> Hi,
>
>    I'm trying to write simple method to change internal value of
> String class:
>
> class String
>     def change!
>         self="0" if self.downcase=="myvalue1"
>         self="1" if self.downcase=="myvalue2"
>         self
>     end
> end
>
> test="myvalue1"
> test.change!
> p test
>
> It should just change value of string to what I want. But I get error:
> "Can't change the value of self".
> How do I proceed?
>
> Thanks.

use String#replace instead of assignment. Beware of the implications!
(i.e. you are changing the string the variable points to!) for
example:

...

test = "myvalue1"
foo = test
bar = test

test.change!

p foo #=> "0"
p bar #-=> "0"

If you don't want this behaviour return new value instead of replace
and do an assigment to variable:

test = test.change