From: "Steve [RubyTalk]" Date: 2005-12-16T19:09:17+09:00 Subject: Re: Question of reference and (sub)strings. Ron Jeffries wrote: >> I might be wrong - but I'm pretty sure that substrings in ruby are >> created with copy-on-write. That is, when you take a substring, a new >> block of memory isn't allocated to the new String, it references the >> same block of memory as the original string - the allocation of a new >> block of memory only occurs when one of the strings is modified. >> > I asked about this a week or two ago, as part of my Extended Set Theory thing, > and the impression I got from the answers then was that sub /arrays/ are copy on > write but that sub /strings/ were not. > Ah-ha... Hmmm - that suggestion "puts the cat among the pigeons" - as far as I can tell, I can't have an array of bytes represented in contiguous memory... (arrays are always arrays of arbitrarily typed objects - aren't they?) - so if copy on write is limited to arrays, it is back to the drawing board for me. I think arrays of objects would necessarily be copy on write as a consequence of the Ruby model of references... Conversely String values are something entirely different - while substrings could be done 'copy on write' - it would necessarily use an entirely separate mechanism. It is obvious that whole strings behave as if they are "copy on write." > I wonder how one might confirm this, other than by asking matz or reading the > compiler and libraries ... I'm coming towards the opinion that I will need to "ask Matz" - though I hope to have exhausted all the other avenues before I resort to bugging him in person. I realise that I could (in principle) read the source code for the ruby interpreter - but, ideally, I'd also like to know how it is intended to work (in order that I can be confident that the details will not change in a future release.) I also presume it would take me quite a while to get to grips with the interpreter source.