From: "F. Senault" Date: 2009-01-22T22:53:22+09:00 Subject: Re: String doesnt auto dup on modification (Ooops, sorry if this message makes it through in duplicate with a "strange" from, but I mixed up my "identities" in my newsreader...) Le Thu, 22 Jan 2009 00:05:58 -0500, RK Sentinel a �crit : > Tom Cloyd wrote: >> If this is an utterly dumb question, just ignore it. However, I AM >> perplexed by this response. Here's why: > i agree with you. > > I have objects that get a string, process/clean it for printing/display. > (That is the whole purpose of centralizing data and behaviour into > classes.) You could also design some "clever" accessors, like this : class Class def attr_duplicator(*members) members.each do |m| self.class_eval(<<-EOM) def #{m} @#{m}.dup end def #{m}=(v) @#{m} = v.dup end EOM end end nil end (Could even bail if the writer is passed something else than a string.) A long winded example : >> class Blah ; attr_duplicator :blix ; end => nil >> t = Blah.new() => # >> z = "abc" => "abc" >> z.__id__ => 12031220 >> t.blix = z => "abc" >> t.instance_variable_get("@blix").__id__ => 12015560 >> t.blix.__id__ => 11609640 >> t.blix.__id__ => 11604360 >> t.blix << "aaa" => "abcaaa" >> t.blix => "abc" >> a = t.blix => "abc" >> a << "aaa" => "abcaaa" >> t.blix => "abc" As usual, not sure about the performance impact, though, especially if you manipulate very big string objects... Fred -- So I open my door to my enemies And I ask could we wipe the slate clean But they tell me to please go fuck myself You know you just can't win (Pink Floyd, Lost For Words)