From: Robert Klemme Date: 2009-01-22T16:28:25+09:00 Subject: Re: String doesnt auto dup on modification On 21.01.2009 22:57, Brian Candler wrote: > RK Sentinel wrote: >> Anyway, through trial and error, I start dup()'ing strings myself. I am >> aware of freeze(). >> >> But would like to know how others handle this generally in large apps. >> >> - Do you keep freezing Strings you make in your classes to avoid >> accidental change >> >> - Do you habitually dup() your string ? > > Generally, no. Same here. > Of course there is no contract to enforce this, but in many cases it > would be considered bad manners to modify an object which is passed in > as an argument. Depends: for example, if you have a method that is supposed to dump something to a stream (IO and friends) which only uses << you can as well use String there. > If you only read the object, then it doesn't matter. That may be true for methods but if you need to store a String as instance variable then I tend to dup it if the application is larger. You can even automate conditional dup'ing by doing something like this class Object def dupf frozen? ? self : dup end end and then class MyClass def initialize(name) @name = name.dupf end end Kind regards robert -- remember.guy do |as, often| as.you_can - without end