Problem with String.my_custom_func! "Can't change the value

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:

self = ......

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

Here’s a trivial example:

class String
def my_func!
self.gsub!(/x/, ‘y’) #okay
self = result_of_tidy_call #not okay
end
end

I’m new enough to ruby that I can’t figure out how to work around
this…

Suggestions?

Thanks!

-philip

Hi –

On Sat, 22 Apr 2006, Philip H. wrote:

I’m new enough to ruby that I can’t figure out how to work around this…

Suggestions?

There’s a String#replace method:

s = “abc”
s.replace(“def”)

s is now the same object, but with different contents.

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! Ruby for Rails
Paper version coming in early May!