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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
48124635945b45221ba12a26371f9e3e?d=identicon&s=25 Philip Hallstrom (Guest)
on 2006-04-22 01:14
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-22 01:14
(Received via mailing list)
Hi --

On Sat, 22 Apr 2006, Philip Hallstrom 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 (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale!  http://www.manning.com/black
Paper version coming in early May!
This topic is locked and can not be replied to.