From: Robert Klemme Date: 2009-01-22T22:25:52+09:00 Subject: Re: String doesnt auto dup on modification 2009/1/22 Stefan Rusterholz : > Robert Klemme wrote: >> class MyClass >> def initialize(name) >> @name = name.dupf >> end >> end > > I'd vote against this. It looks to me like a great way to confuse users > and complicate interfaces. I'd rather go towards transparency. > Generally I'd not mutate arguments, only the receiver. If there's a > valid case to mutate an argument, it should be documented and evident. > The user then has to provide a duplicate if he still needs the original. Not sure what you mean because the code makes sure that the argument is *not* mutated. Or did you mean "caller" when you wrote "I" above? There are different policies to handle that as this thread has shown. Generally I tend to follow the line that the caller is the owner of the object unless otherwise stated. This also means that he is free to change it at any time and consequently it is the responsibility of the receiver to take whatever measures if he needs to make sure he keeps track of the unmodified state. IMHO this is a reasonable policy because if ownership passing would be the default you would have to copy always as the caller if you need to continue to work with the object - even if receivers just use the object temporary and do not store references for longer. This would be a waste of resources. In practice this is usually not an issue for me so IMHO the discussion has a strong "theoretical" aspect. OTOH it's good to reason about these things from time to time. Helps clarify some things and avoid mistakes. Cheers robert -- remember.guy do |as, often| as.you_can - without end