From: Lars Christensen Date: 2008-05-06T00:40:04+09:00 Subject: String copy-on-write question Hello group, Ruby implements copy-on-write for strings, so you can do stuff like this very cheaply: str = 0.chr * (2**24) # 16MiB allocated str[100..-1] # this costs only a small amount of memory How come this optimization does not apply in this case?: str[100..-2] # this costs around 16MiB bytes of memory As a side effect, if using regexps on a large string, the pre-match and post-match variables behave differently: s = 0.chr * (2**23) + "Hello" + 0.chr * (2**23) # About 16MiB allocated (after GC) s.scan(/Hello/) { |m| p m } # This is free p $'.size # This is free p $`.size # This costs another 8MiB. Any insights? Lars