# Problem with String.my\_custom\_func! "Can't change the value of self"

**URL:** https://rubytalk.org/t/problem-with-string-my-custom-func-cant-change-the-value-of-self/26830
**Category:** ruby-talk
**Created:** [21 April 2006 23:12 UTC](https://rubytalk.org/t/problem-with-string-my-custom-func-cant-change-the-value-of-self/26830 "2006-04-21T23:12:37Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Philip\_Hallstrom](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@Philip\_Hallstrom](https://rubytalk.org/u/Philip_Hallstrom)
#### Post date: [21 April 2006 23:12 UTC](https://rubytalk.org/t/problem-with-string-my-custom-func-cant-change-the-value-of-self/26830/1 "2006-04-21T23:12:37Z")

</div>

Hi all -

I'm extending the String class to include a tidy\_up\_html() method that runs the string using tidy. Works great. However, I'd like to also have a tidy\_up\_html!() method and do it in place.

The problem is that the tidy library \*has\* to return a new value, so at some point I have to do:

&nbsp;&nbsp;&nbsp;self = ......

at which point I get the error: Can't change the value of self

Here's a trivial example:

class String  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def my\_func!  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.gsub!(/x/, 'y') #okay  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = result\_of\_tidy\_call #not okay  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end

I'm new enough to ruby that I can't figure out how to work around this...

Suggestions?

Thanks!

-philip

---

<div class="post-metadata">

### Author: ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)
#### Post date: [21 April 2006 23:14 UTC](https://rubytalk.org/t/problem-with-string-my-custom-func-cant-change-the-value-of-self/26830/2 "2006-04-21T23:14:13Z")

</div>

Hi --

> **···**
>
> On Sat, 22 Apr 2006, Philip Hallstrom wrote:
> 
> > Hi all -
> > 
> > I'm extending the String class to include a tidy\_up\_html() method that runs the string using tidy. Works great. However, I'd like to also have a tidy\_up\_html!() method and do it in place.
> > 
> > The problem is that the tidy library \*has\* to return a new value, so at some point I have to do:
> > 
> > &nbsp;&nbsp;self = ......
> > 
> > at which point I get the error: Can't change the value of self
> > 
> > Here's a trivial example:
> > 
> > class String  
> > &nbsp;&nbsp;&nbsp;def my\_func!  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.gsub!(/x/, 'y') #okay  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self = result\_of\_tidy\_call #not okay  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > I'm new enough to ruby that I can't figure out how to work around this...
> > 
> > Suggestions?
> 
> There's a String#replace method:
> 
> &nbsp;&nbsp;&nbsp;s = "abc"  
> &nbsp;&nbsp;&nbsp;s.replace("def")
> 
> s is now the same object, but with different contents.
> 
> David
> 
> --  
> David A. Black (dblack@wobblini.net)  
> Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))
> 
> "Ruby for Rails" PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
> Paper version coming in early May!
