From: cart4for1@... Date: 2021-03-27T07:45:41+00:00 Subject: [ruby-core:103052] [Ruby master Misc#17751] Do these instructions (<<, +, [0..n]) modify the original string without creating copies? Issue #17751 has been updated by stiuna (Juan Gregorio). xtkoba (Tee KOBAYASHI) wrote in #note-3: > I would not even concatenate any strings and would `push` them to an array, as if they were immutable (like in Go language for example). But in the end that information will have to be written to a file so I will have to use 'join' and you see, copies are made. mame (Yusuke Endoh) wrote in #note-4: > Note, however, that these operations (String#prepend and []=) may take a long time. They do not create another huge string, but they copy the whole content in place, which may take O(n). When you say **"may take a long time"** does it apply even if **'str'** is 3GB and **'header'** is only 5bits? or do you mean only when both variables are large? Another solution to my problem would be to be able to write data to a file from any position I want (in bits) by replacing its content. For example if the binary of a **.txt** is the following: ``` "00101111" ``` And I say: ``` ruby file.writeSince(5, "00111100011") ``` The **.txt** file would change to this: ``` "00101001_11100011" ``` That way the last 3 bits of the **.txt** file were overwritten with the first 3 bits that **'writeSince'** received, with this I don't need to concatenate to **'header'** and **'str'**. Only in the future concatenation will be mandatory but in the short term what I said above is my priority. ---------------------------------------- Misc #17751: Do these instructions (<<,+,[0..n]) modify the original string without creating copies? https://bugs.ruby-lang.org/issues/17751#change-91113 * Author: stiuna (Juan Gregorio) * Status: Open * Priority: Normal ---------------------------------------- In my program a string increases considerably in size inside a loop, at the end of that loop a header is created that will have to go to the beginning of that string. During the whole loop: ``` ruby str << "some data" ``` At the end: ``` ruby header = "other data" str = header + str ``` I understand that using (+) creates a copy to then modify the original variable, that is not desirable, I would like to do something similar to (<<), which I understand does not create copies. If I do this: ``` ruby header << str ``` I would have two variables with a very large size. I also have this other code and I don't know if it is an "in place" modifier: ``` ruby str = "12345" str[0..2] = "" #s => 45 ``` In short, I want to know what instructions I should use to remove a given range from a string and how to concatenate to both the beginning and end of the target string without having to create copies. -- https://bugs.ruby-lang.org/ Unsubscribe: