From: Stefano Crocco Date: 2011-05-26T04:28:39+09:00 Subject: Re: Method that mutates object On Thursday 26 May 2011 04:22:46 jason solomon wrote: > Say we want to write a String method called clear that takes a given > string and modifies that string to be equal to "". > > Ex. > > str = "string" > > we call str.clear and we get back "", not just "" printed to the screen, > but the value of str is now "" (mutate the original string). > > > I understand that the following code would just print "" to the screen > but not modify the actual str object. > > class String > def clear > "" > end > end > > > How would you write a method that actually modifies the str object? This should do what you want class String def clear replace '' end end Stefano