From: Peter Zotov Date: 2011-07-26T23:23:41+09:00 Subject: Re: Why aren't string literals treated similarly to symbols? On Tue, 26 Jul 2011 16:28:54 +0900, Robert Klemme wrote: >> One possible fix would be something like: >> >> string = "example" >> 1.upto(2) do >> puts string >> end >> >> In this trivial case, the solution is good enough, but the general >> case >> requires more knowledge about how to optimize for the interpreter >> than >> should be necessary. I'm probably missing something obvious, so if >> anyone knows what it is, please point it out. Maybe MRI already >> does >> this obvious thing. > > It's doing COW already internally. > > http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_9_2/string.c?revision=31809&view=markup > > See e.g. lines 1984ff. Then functions > > rb_str_resize > rb_str_modify > str_independent > str_make_independent > > Kind regards > > robert Just wanted to mention that RXR is a bit more convenient to lookup things like this. http://rxr.whitequark.org/mri/source/string.c#1761 And it has identifier search! -- WBR, Peter Zotov.