# String#\[\]= method

**URL:** <https://rubytalk.org/t/string-method/73675>\
**Category:** ruby-talk\
**Created:** [22 June 2016 12:04 UTC](https://rubytalk.org/t/string-method/73675 "2016-06-22T12:04:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sophie\_Wellow](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Sophie\_Wellow](https://rubytalk.org/u/Sophie_Wellow)\
**Post date:** [22 June 2016 12:04 UTC](https://rubytalk.org/t/string-method/73675/1 "2016-06-22T12:04:57Z")

</div>

This is my first time on a mailing list, so go easy on me.

Hopefully a specific question with a simple yes/no answer:

From the docs for String#[offset,length]="blah" it seemed to imply that if  
the length of the new fragment was the same as the length being replaced,  
nothing would need to be re-allocated.

Yet looking through the source (briefly) it seems like no mater what, then  
entire string gets built from scratch every time.

Can anyone confirm:

a) that I've interpreted things correctly and a new string is built every  
time, regardless of the lengths matching.

b) If that is the case, is there in fact any way to replace a like sized  
fragment of a string that does indeed simply splat (technical term) the new  
data over the old data.

ps. I know I can use String#setbyte on a byte by byte basis, but that would  
be a pain if wanted to replace a 5k fragment in the middle of a 20mb string.

pps. Also happy to hear that I've completely misunderstood the  
String#[num,num]='xxx' code.

Thanks.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [22 June 2016 14:03 UTC](https://rubytalk.org/t/string-method/73675/2 "2016-06-22T14:03:14Z")

</div>

> This is my first time on a mailing list, so go easy on me.

Welcome!

> Hopefully a specific question with a simple yes/no answer:
> 
> From the docs for String#[offset,length]="blah" it seemed to imply that if  
> the length of the new fragment was the same as the length being replaced,  
> nothing would need to be re-allocated.
> 
> Yet looking through the source (briefly) it seems like no mater what, then  
> entire string gets built from scratch every time.
> 
> Can anyone confirm:
> 
> a) that I've interpreted things correctly and a new string is built every  
> time, regardless of the lengths matching.

IIRC String uses copy on write internally so it has to create a new  
byte array internally - at least if the byte array is shared. There is  
a flag STR\_SHARED: [https://github.com/ruby/ruby/blob/trunk/string.c](https://github.com/ruby/ruby/blob/trunk/string.c)

> b) If that is the case, is there in fact any way to replace a like sized  
> fragment of a string that does indeed simply splat (technical term) the new  
> data over the old data.

Theoretically it would be possible if the byte array was not shared.  
Quick look seems to indicate it's done like that.  
see [https://github.com/ruby/ruby/blob/trunk/string.c#L1902](https://github.com/ruby/ruby/blob/trunk/string.c#L1902)

> ps. I know I can use String#setbyte on a byte by byte basis, but that would  
> be a pain if wanted to replace a 5k fragment in the middle of a 20mb string.

Even that might create a new internal byte array because of copy on  
write (see above).

> pps. Also happy to hear that I've completely misunderstood the  
> String#[num,num]='xxx' code.

I don't think you have to worry about these internals. Why do you  
think you need?

Kind regards

robert

> **···**
>
> On Wed, Jun 22, 2016 at 2:04 PM, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [22 June 2016 13:31 UTC](https://rubytalk.org/t/string-method/73675/3 "2016-06-22T13:31:18Z")

</div>

Hi Sophie,

> This is my first time on a mailing list, so go easy on me.
> 
> Hopefully a specific question with a simple yes/no answer:
> 
> From the docs for String#[offset,length]="blah" it seemed to imply that if  
> the length of the new fragment was the same as the length being replaced,  
> nothing would need to be re-allocated.
> 
> Yet looking through the source (briefly) it seems like no mater what, then  
> entire string gets built from scratch every time.
> 
> Can anyone confirm:
> 
> a) that I've interpreted things correctly and a new string is built every  
> time, regardless of the lengths matching.
> 
> b) If that is the case, is there in fact any way to replace a like sized  
> fragment of a string that does indeed simply splat (technical term) the new  
> data over the old data.
> 
> ps. I know I can use String#setbyte on a byte by byte basis, but that  
> would be a pain if wanted to replace a 5k fragment in the middle of a 20mb  
> string.
> 
> pps. Also happy to hear that I've completely misunderstood the  
> String#[num,num]='xxx' code.
> 
> Thanks.

​Looking in the 2.3.0 documentation  
\<[Class: String (Ruby 2.3.0)](http://ruby-doc.org/core-2.3.0/String.html#method-i-5B-5D-3D&gt); I only see  
this: "​If the replacement string is not the same length as the text it is  
replacing, the string will be adjusted accordingly." To me, that just says  
that the String will grow or shrink appropriately to match the replacement;  
it says nothing about reallocations, which is presumably an implementation  
detail.

To that end, 'ruby' (aka "MRI" or "CRuby") is only one implementation of  
the Ruby language; maybe jruby or rubinius do something different under the  
hood. (I'd bet quite strongly that jruby does 😉)

That said, from what I can see in string.c in trunk  
\<[https://github.com/ruby/ruby/blob/trunk/string.c#L4163&gt](https://github.com/ruby/ruby/blob/trunk/string.c#L4163&gt); (i.e ruby 2.4) MRI  
does appear to be reusing the same physical memory location for the string:  
shuffling the tail to the left or right if necessary, then blitting the  
replacement value. (I didn't track down what happens when the range  
parameter is a regexp.)

Cheers

> **···**
>
> On 22 June 2016 at 22:04, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:  
> --  
> &nbsp;&nbsp;Matthew Kerwin  
> &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)

---

<div class="post-metadata">

**Author:** ![Sophie\_Wellow](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Sophie\_Wellow](https://rubytalk.org/u/Sophie_Wellow)\
**Post date:** [22 June 2016 14:08 UTC](https://rubytalk.org/t/string-method/73675/4 "2016-06-22T14:08:25Z")

</div>

That's the doc fragment indeed. I'd taken "If the replacement string is  
not the same length as the text it is replacing, the string will be  
adjusted accordingly." to imply that if the replacement string WAS the same  
size then the string would not be "adjusted", but I guess I'd inferred too  
much into that.

I can't get 2.4 to install yet... but I'll try it as soon as it does.

Thanks so much.

> **···**
>
> On Wed, Jun 22, 2016 at 2:31 PM, Matthew Kerwin \<matthew@kerwin.net.au\> wrote:
> 
> > Hi Sophie,
> > 
> > On 22 June 2016 at 22:04, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> > 
> > > This is my first time on a mailing list, so go easy on me.
> > > 
> > > Hopefully a specific question with a simple yes/no answer:
> > > 
> > > From the docs for String#[offset,length]="blah" it seemed to imply that  
> > > if the length of the new fragment was the same as the length being  
> > > replaced, nothing would need to be re-allocated.
> > > 
> > > Yet looking through the source (briefly) it seems like no mater what,  
> > > then entire string gets built from scratch every time.
> > > 
> > > Can anyone confirm:
> > > 
> > > a) that I've interpreted things correctly and a new string is built every  
> > > time, regardless of the lengths matching.
> > > 
> > > b) If that is the case, is there in fact any way to replace a like sized  
> > > fragment of a string that does indeed simply splat (technical term) the new  
> > > data over the old data.
> > > 
> > > ps. I know I can use String#setbyte on a byte by byte basis, but that  
> > > would be a pain if wanted to replace a 5k fragment in the middle of a 20mb  
> > > string.
> > > 
> > > pps. Also happy to hear that I've completely misunderstood the  
> > > String#[num,num]='xxx' code.
> > > 
> > > Thanks.
> > 
> > ​Looking in the 2.3.0 documentation  
> > \<[Class: String (Ruby 2.3.0)](http://ruby-doc.org/core-2.3.0/String.html#method-i-5B-5D-3D&gt); I only see  
> > this: "​If the replacement string is not the same length as the text it  
> > is replacing, the string will be adjusted accordingly." To me, that just  
> > says that the String will grow or shrink appropriately to match the  
> > replacement; it says nothing about reallocations, which is presumably an  
> > implementation detail.
> > 
> > To that end, 'ruby' (aka "MRI" or "CRuby") is only one implementation of  
> > the Ruby language; maybe jruby or rubinius do something different under the  
> > hood. (I'd bet quite strongly that jruby does 😉)
> > 
> > That said, from what I can see in string.c in trunk  
> > \<[https://github.com/ruby/ruby/blob/trunk/string.c#L4163&gt](https://github.com/ruby/ruby/blob/trunk/string.c#L4163&gt); (i.e ruby 2.4)  
> > MRI does appear to be reusing the same physical memory location for the  
> > string: shuffling the tail to the left or right if necessary, then blitting  
> > the replacement value. (I didn't track down what happens when the range  
> > parameter is a regexp.)
> > 
> > Cheers  
> > --  
> > &nbsp;&nbsp;Matthew Kerwin  
> > &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Sophie\_Wellow](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Sophie\_Wellow](https://rubytalk.org/u/Sophie_Wellow)\
**Post date:** [22 June 2016 23:11 UTC](https://rubytalk.org/t/string-method/73675/5 "2016-06-22T23:11:02Z")

</div>

Thanks for the replies - the reason I am interested is that I'm amusing  
myself by writing a simple little image drawing library - just for my use  
as an exercise. I'm using strings to hold the binary image data. I was  
brought up on C/C++ so I'm all too aware of the kind of stuff going on  
under the hood and that speed won't be super..... having said that it still  
seems a bit heavy that it may not even be possible to change a single byte  
in an 8Mb string without a complete copy / reallocation each time... If  
there's a better basic class to use than String then I'd happily use that  
instead. 🙂

> **···**
>
> On Wed, Jun 22, 2016 at 3:03 PM, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On Wed, Jun 22, 2016 at 2:04 PM, Sophie Wellow \<sophie.wellow@gmail.com\> \> wrote:  
> > \> This is my first time on a mailing list, so go easy on me.
> > 
> > Welcome!
> > 
> > \> Hopefully a specific question with a simple yes/no answer:  
> > \>  
> > \> From the docs for String#[offset,length]="blah" it seemed to imply that  
> > if  
> > \> the length of the new fragment was the same as the length being replaced,  
> > \> nothing would need to be re-allocated.  
> > \>  
> > \> Yet looking through the source (briefly) it seems like no mater what,  
> > then  
> > \> entire string gets built from scratch every time.  
> > \>  
> > \> Can anyone confirm:  
> > \>  
> > \> a) that I've interpreted things correctly and a new string is built every  
> > \> time, regardless of the lengths matching.
> > 
> > IIRC String uses copy on write internally so it has to create a new  
> > byte array internally - at least if the byte array is shared. There is  
> > a flag STR\_SHARED: [https://github.com/ruby/ruby/blob/trunk/string.c](https://github.com/ruby/ruby/blob/trunk/string.c)
> > 
> > \> b) If that is the case, is there in fact any way to replace a like sized  
> > \> fragment of a string that does indeed simply splat (technical term) the  
> > new  
> > \> data over the old data.
> > 
> > Theoretically it would be possible if the byte array was not shared.  
> > Quick look seems to indicate it's done like that.  
> > see [https://github.com/ruby/ruby/blob/trunk/string.c#L1902](https://github.com/ruby/ruby/blob/trunk/string.c#L1902)
> > 
> > \> ps. I know I can use String#setbyte on a byte by byte basis, but that  
> > would  
> > \> be a pain if wanted to replace a 5k fragment in the middle of a 20mb  
> > string.
> > 
> > Even that might create a new internal byte array because of copy on  
> > write (see above).
> > 
> > \> pps. Also happy to hear that I've completely misunderstood the  
> > \> String#[num,num]='xxx' code.
> > 
> > I don't think you have to worry about these internals. Why do you  
> > think you need?
> > 
> > Kind regards
> > 
> > robert
> > 
> > --  
> > [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> > - without end}  
> > [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [23 June 2016 07:32 UTC](https://rubytalk.org/t/string-method/73675/6 "2016-06-23T07:32:22Z")

</div>

I do not have the kind of intimate familiarity with Ruby MRI's C code  
and I am lacking time to establish it to give definitive answers. Did  
you verify that what you claim above is true under all circumstances?  
Because, from the brief inspection that I did yesterday I believe that  
there are situations where the data sitting inside the String instance  
is \_not\_ copied. This is a quite obvious optimization for the case  
that String data is not shared with another String instance so I would  
be very surprised if that was not implemented.

Apart from that, if you write an image manipulating library an  
alternative approach is to not (ab) use String for this but create  
your own class with proper access and conversion methods. Then you can  
optimize memory handling the way you need it to be. You can even  
provide a similar set of methods that String offers (e.g. each\_byte)  
to make switching easier. Whether that approach is better in your case  
I don't know. But this is an option that you could consider.

Kind regards

robert

> **···**
>
> On Thu, Jun 23, 2016 at 1:11 AM, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> 
> > Thanks for the replies - the reason I am interested is that I'm amusing  
> > myself by writing a simple little image drawing library - just for my use as  
> > an exercise. I'm using strings to hold the binary image data. I was  
> > brought up on C/C++ so I'm all too aware of the kind of stuff going on under  
> > the hood and that speed won't be super..... having said that it still seems  
> > a bit heavy that it may not even be possible to change a single byte in an  
> > 8Mb string without a complete copy / reallocation each time... If there's a  
> > better basic class to use than String then I'd happily use that instead. 🙂
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)\
**Post date:** [23 June 2016 20:29 UTC](https://rubytalk.org/t/string-method/73675/7 "2016-06-23T20:29:46Z")

</div>

give NArray a look [[http://masa16.github.io/narray/\](http://masa16.github.io/narray/%5C)]

martin

> **···**
>
> On Wed, Jun 22, 2016 at 4:11 PM, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> 
> > Thanks for the replies - the reason I am interested is that I'm amusing  
> > myself by writing a simple little image drawing library - just for my use  
> > as an exercise. I'm using strings to hold the binary image data. I was  
> > brought up on C/C++ so I'm all too aware of the kind of stuff going on  
> > under the hood and that speed won't be super..... having said that it still  
> > seems a bit heavy that it may not even be possible to change a single byte  
> > in an 8Mb string without a complete copy / reallocation each time... If  
> > there's a better basic class to use than String then I'd happily use that  
> > instead. 🙂

---

<div class="post-metadata">

**Author:** ![Sophie\_Wellow](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Sophie\_Wellow](https://rubytalk.org/u/Sophie_Wellow)\
**Post date:** [23 June 2016 09:47 UTC](https://rubytalk.org/t/string-method/73675/8 "2016-06-23T09:47:46Z")

</div>

Thanks for that - will take it on board. I finally manages to make some  
simple sample code reliably be "strange":

require 'benchmark'  
class Bob  
&nbsp;&nbsp;SAMPLE = 4  
&nbsp;&nbsp;def initialize( w, h )  
&nbsp;&nbsp;@w = w  
&nbsp;&nbsp;@h = h  
&nbsp;&nbsp;&nbsp;&nbsp;@buffer = "\x00"\*(@w\*@h\*SAMPLE)  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def fill1  
&nbsp;&nbsp;&nbsp;&nbsp;line = "\x01\x02\x03\x04" \* @w  
&nbsp;&nbsp;#line = [0x01020304].pack("L\<")\* @w  
&nbsp;&nbsp;&nbsp;&nbsp;@h.times do |y|  
&nbsp;&nbsp;@buffer[y\*@w\*SAMPLE,@w\*SAMPLE] = line  
&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def fill2  
&nbsp;&nbsp;&nbsp;&nbsp;#line = "\x01\x02\x03\x04" \* @w  
&nbsp;&nbsp;line = [0x01020304].pack("L\<")\* @w  
&nbsp;&nbsp;&nbsp;&nbsp;@h.times do |y|  
&nbsp;&nbsp;@buffer[y\*@w\*SAMPLE,@w\*SAMPLE] = line  
&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

b = Bob.new( 2048, 1024 )  
Benchmark.bm(10) do |x|  
x.report('fill1') { b.fill1 }  
x.report('fill2') { b.fill2 }  
end

On Ruby 1.9.3:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real

fill1 0.000000 0.000000 0.000000 ( 0.001082)

fill2 0.530000 0.000000 0.530000 ( 0.535084)

On Ruby 2.3.1:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real

fill1 0.850000 0.000000 0.850000 ( 0.843473)

fill2 0.770000 0.000000 0.770000 ( 0.777828)

Notice that the only difference between fill1 and fill2 is how it made the  
replacement fragment - not the size or content of the fragment.

Notice also that my nice fast fill1 on 1.9.3 is now dead as a dodo on 2.3.1

Whilst I'm not wanting to get bogged down in fine tuning, the difference  
between 0.0 seconds and 0.8 seconds is a little concerning.

All very curious. Please don't wade through tonnes of code looking at  
this, it's just a play thing, but any quick observations would be  
interesting.

> **···**
>
> On Thu, Jun 23, 2016 at 8:32 AM, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On Thu, Jun 23, 2016 at 1:11 AM, Sophie Wellow \<sophie.wellow@gmail.com\> \> wrote:  
> > \> Thanks for the replies - the reason I am interested is that I'm amusing  
> > \> myself by writing a simple little image drawing library - just for my  
> > use as  
> > \> an exercise. I'm using strings to hold the binary image data. I was  
> > \> brought up on C/C++ so I'm all too aware of the kind of stuff going on  
> > under  
> > \> the hood and that speed won't be super..... having said that it still  
> > seems  
> > \> a bit heavy that it may not even be possible to change a single byte in  
> > an  
> > \> 8Mb string without a complete copy / reallocation each time... If  
> > there's a  
> > \> better basic class to use than String then I'd happily use that instead.  
> > 🙂
> > 
> > I do not have the kind of intimate familiarity with Ruby MRI's C code  
> > and I am lacking time to establish it to give definitive answers. Did  
> > you verify that what you claim above is true under all circumstances?  
> > Because, from the brief inspection that I did yesterday I believe that  
> > there are situations where the data sitting inside the String instance  
> > is \_not\_ copied. This is a quite obvious optimization for the case  
> > that String data is not shared with another String instance so I would  
> > be very surprised if that was not implemented.
> > 
> > Apart from that, if you write an image manipulating library an  
> > alternative approach is to not (ab) use String for this but create  
> > your own class with proper access and conversion methods. Then you can  
> > optimize memory handling the way you need it to be. You can even  
> > provide a similar set of methods that String offers (e.g. each\_byte)  
> > to make switching easier. Whether that approach is better in your case  
> > I don't know. But this is an option that you could consider.
> > 
> > Kind regards
> > 
> > robert
> > 
> > --  
> > [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> > - without end}  
> > [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![A\_Berger](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@A\_Berger](https://rubytalk.org/u/A_Berger)\
**Post date:** [23 June 2016 10:27 UTC](https://rubytalk.org/t/string-method/73675/9 "2016-06-23T10:27:43Z")

</div>

Curious:  
no output with @w=100 : (JRuby)

b = Bob.new( 2048\*100, 1024 )

How that?  
Berg

---

<div class="post-metadata">

**Author:** ![A\_Berger](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@A\_Berger](https://rubytalk.org/u/A_Berger)\
**Post date:** [23 June 2016 10:29 UTC](https://rubytalk.org/t/string-method/73675/10 "2016-06-23T10:29:07Z")

</div>

...meant @w=2048\*100

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [23 June 2016 11:21 UTC](https://rubytalk.org/t/string-method/73675/11 "2016-06-23T11:21:18Z")

</div>

​Interesting. What's the encoding of the string in fill1? What happens if  
you do this?

line = "\x01\x02\x03\x04"  
.b  
\* @w

Or if you add the "  
# encoding: BINARY  
" magic comment on line 1 of your rb file? Sorry I can't test it myself,  
wrong computer.

Cheers

> **···**
>
> On 23 June 2016 at 19:47, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> 
> > Thanks for that - will take it on board. I finally manages to make some  
> > simple sample code reliably be "strange":
> > 
> > ​\<snip\>
> > 
> > Notice that the only difference between fill1 and fill2 is how it made the  
> > replacement fragment - not the size or content of the fragment.
> > 
> > Notice also that my nice fast fill1 on 1.9.3 is now dead as a dodo on 2.3.1
> > 
> > Whilst I'm not wanting to get bogged down in fine tuning, the difference  
> > between 0.0 seconds and 0.8 seconds is a little concerning.
> > 
> > All very curious. Please don't wade through tonnes of code looking at  
> > this, it's just a play thing, but any quick observations would be  
> > interesting.
> 
> --  
> &nbsp;&nbsp;Matthew Kerwin  
> &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)

---

<div class="post-metadata">

**Author:** ![Sophie\_Wellow](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Sophie\_Wellow](https://rubytalk.org/u/Sophie_Wellow)\
**Post date:** [23 June 2016 10:39 UTC](https://rubytalk.org/t/string-method/73675/12 "2016-06-23T10:39:09Z")

</div>

You're asking for almost a Gig of memory there...

> **···**
>
> On Thu, Jun 23, 2016 at 11:29 AM, A Berger \<aberger7890@gmail.com\> wrote:
> 
> > ...meant @w=2048\*100
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Sophie\_Wellow](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Sophie\_Wellow](https://rubytalk.org/u/Sophie_Wellow)\
**Post date:** [23 June 2016 10:43 UTC](https://rubytalk.org/t/string-method/73675/13 "2016-06-23T10:43:57Z")

</div>

On 1.9.3, in your case, I get:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real

fill1 0.080000 0.000000 0.080000 ( 0.073460)

fill2 61.350000 0.260000 61.610000 ( 61.977526)

> **···**
>
> On Thu, Jun 23, 2016 at 11:39 AM, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> 
> > You're asking for almost a Gig of memory there...
> > 
> > On Thu, Jun 23, 2016 at 11:29 AM, A Berger \<aberger7890@gmail.com\> wrote:
> > 
> > > ...meant @w=2048\*100
> > > 
> > > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Daniel\_Ferreira](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@Daniel\_Ferreira](https://rubytalk.org/u/Daniel_Ferreira)\
**Post date:** [23 June 2016 11:03 UTC](https://rubytalk.org/t/string-method/73675/14 "2016-06-23T11:03:30Z")

</div>

Hi Sophie,

I believe that if we are seeing performance degradation and possibly  
missing functionality it would be worth to raise the issue in ruby-core  
mailing list.  
Core developers will be able to discuss it properly and address any issue  
that may exist.

Contributions like yours are very well welcome.

If it turns out not to be a bug but rather an actually desired feature the  
bug issue will be closed otherwise actions will be put forward in order to  
resolve it.

Feel free to interact directly with the ruby core for questions of this  
level.

They are very friendly and even Matz itself replies very often to questions  
like yours.

Happy ruby coding,  
Daniel

> **···**
>
> On Thursday, 23 June 2016, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> 
> > On 1.9.3, in your case, I get:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real
> > 
> > fill1 0.080000 0.000000 0.080000 ( 0.073460)
> > 
> > fill2 61.350000 0.260000 61.610000 ( 61.977526)
> > 
> > On Thu, Jun 23, 2016 at 11:39 AM, Sophie Wellow \<sophie.wellow@gmail.com \> \<javascript:\_e(%7B%7D,'cvml','sophie.wellow@gmail.com');\>\> wrote:
> > 
> > > You're asking for almost a Gig of memory there...
> > > 
> > > On Thu, Jun 23, 2016 at 11:29 AM, A Berger \<aberger7890@gmail.com \>\> \<javascript:\_e(%7B%7D,'cvml','aberger7890@gmail.com');\>\> wrote:
> > > 
> > > > ...meant @w=2048\*100
> > > > 
> > > > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org  
> > > > \<javascript:\_e(%7B%7D,'cvml','ruby-talk-request@ruby-lang.org');\>  
> > > > ?subject=unsubscribe\>  
> > > > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Sophie\_Wellow](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Sophie\_Wellow](https://rubytalk.org/u/Sophie_Wellow)\
**Post date:** [23 June 2016 11:23 UTC](https://rubytalk.org/t/string-method/73675/15 "2016-06-23T11:23:37Z")

</div>

Cripes:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real

fill1 0.000000 0.000000 0.000000 ( 0.001079)

fill2 0.000000 0.000000 0.000000 ( 0.001064)

> **···**
>
> On Thu, Jun 23, 2016 at 12:21 PM, Matthew Kerwin \<matthew@kerwin.net.au\> wrote:
> 
> > On 23 June 2016 at 19:47, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> > 
> > > Thanks for that - will take it on board. I finally manages to make some  
> > > simple sample code reliably be "strange":
> > > 
> > > ​\<snip\>
> > > 
> > > Notice that the only difference between fill1 and fill2 is how it made  
> > > the replacement fragment - not the size or content of the fragment.
> > > 
> > > Notice also that my nice fast fill1 on 1.9.3 is now dead as a dodo on  
> > > 2.3.1
> > > 
> > > Whilst I'm not wanting to get bogged down in fine tuning, the difference  
> > > between 0.0 seconds and 0.8 seconds is a little concerning.
> > > 
> > > All very curious. Please don't wade through tonnes of code looking at  
> > > this, it's just a play thing, but any quick observations would be  
> > > interesting.
> > 
> > ​Interesting. What's the encoding of the string in fill1? What happens if  
> > you do this?
> > 
> > line = "\x01\x02\x03\x04"  
> > .b  
> > \* @w
> > 
> > Or if you add the "  
> > # encoding: BINARY  
> > " magic comment on line 1 of your rb file? Sorry I can't test it myself,  
> > wrong computer.
> > 
> > Cheers  
> > --  
> > &nbsp;&nbsp;Matthew Kerwin  
> > &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Sophie\_Wellow](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Sophie\_Wellow](https://rubytalk.org/u/Sophie_Wellow)\
**Post date:** [23 June 2016 11:23 UTC](https://rubytalk.org/t/string-method/73675/16 "2016-06-23T11:23:58Z")

</div>

ps. That's with the BINARY encoding line

> **···**
>
> On Thu, Jun 23, 2016 at 12:23 PM, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> 
> > Cripes:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real
> > 
> > fill1 0.000000 0.000000 0.000000 ( 0.001079)
> > 
> > fill2 0.000000 0.000000 0.000000 ( 0.001064)
> > 
> > On Thu, Jun 23, 2016 at 12:21 PM, Matthew Kerwin \<matthew@kerwin.net.au\> \> wrote:
> > 
> > > On 23 June 2016 at 19:47, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> > > 
> > > > Thanks for that - will take it on board. I finally manages to make some  
> > > > simple sample code reliably be "strange":
> > > > 
> > > > ​\<snip\>
> > > > 
> > > > Notice that the only difference between fill1 and fill2 is how it made  
> > > > the replacement fragment - not the size or content of the fragment.
> > > > 
> > > > Notice also that my nice fast fill1 on 1.9.3 is now dead as a dodo on  
> > > > 2.3.1
> > > > 
> > > > Whilst I'm not wanting to get bogged down in fine tuning, the difference  
> > > > between 0.0 seconds and 0.8 seconds is a little concerning.
> > > > 
> > > > All very curious. Please don't wade through tonnes of code looking at  
> > > > this, it's just a play thing, but any quick observations would be  
> > > > interesting.
> > > 
> > > ​Interesting. What's the encoding of the string in fill1? What happens if  
> > > you do this?
> > > 
> > > line = "\x01\x02\x03\x04"  
> > > .b  
> > > \* @w
> > > 
> > > Or if you add the "  
> > > # encoding: BINARY  
> > > " magic comment on line 1 of your rb file? Sorry I can't test it myself,  
> > > wrong computer.
> > > 
> > > Cheers  
> > > --  
> > > &nbsp;&nbsp;Matthew Kerwin  
> > > &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)
> > > 
> > > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Sophie\_Wellow](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Sophie\_Wellow](https://rubytalk.org/u/Sophie_Wellow)\
**Post date:** [23 June 2016 11:26 UTC](https://rubytalk.org/t/string-method/73675/17 "2016-06-23T11:26:41Z")

</div>

ps. same on 2.3.1 - nice.

I've only ever had occasional use for the # encoding ... fragment, and I  
didn't even realise that BINARY was an option.

> **···**
>
> On Thu, Jun 23, 2016 at 12:23 PM, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> 
> > ps. That's with the BINARY encoding line
> > 
> > On Thu, Jun 23, 2016 at 12:23 PM, Sophie Wellow \<sophie.wellow@gmail.com\> \> wrote:
> > 
> > > Cripes:
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real
> > > 
> > > fill1 0.000000 0.000000 0.000000 ( 0.001079)
> > > 
> > > fill2 0.000000 0.000000 0.000000 ( 0.001064)
> > > 
> > > On Thu, Jun 23, 2016 at 12:21 PM, Matthew Kerwin \<matthew@kerwin.net.au\> \>\> wrote:
> > > 
> > > > On 23 June 2016 at 19:47, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> > > > 
> > > > > Thanks for that - will take it on board. I finally manages to make  
> > > > > some simple sample code reliably be "strange":
> > > > > 
> > > > > ​\<snip\>
> > > > > 
> > > > > Notice that the only difference between fill1 and fill2 is how it made  
> > > > > the replacement fragment - not the size or content of the fragment.
> > > > > 
> > > > > Notice also that my nice fast fill1 on 1.9.3 is now dead as a dodo on  
> > > > > 2.3.1
> > > > > 
> > > > > Whilst I'm not wanting to get bogged down in fine tuning, the  
> > > > > difference between 0.0 seconds and 0.8 seconds is a little concerning.
> > > > > 
> > > > > All very curious. Please don't wade through tonnes of code looking at  
> > > > > this, it's just a play thing, but any quick observations would be  
> > > > > interesting.
> > > > 
> > > > ​Interesting. What's the encoding of the string in fill1? What happens  
> > > > if you do this?
> > > > 
> > > > line = "\x01\x02\x03\x04"  
> > > > .b  
> > > > \* @w
> > > > 
> > > > Or if you add the "  
> > > > # encoding: BINARY  
> > > > " magic comment on line 1 of your rb file? Sorry I can't test it myself,  
> > > > wrong computer.
> > > > 
> > > > Cheers  
> > > > --  
> > > > &nbsp;&nbsp;Matthew Kerwin  
> > > > &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)
> > > > 
> > > > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org  
> > > > ?subject=unsubscribe\>  
> > > > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![A\_Berger](https://avatars.discourse-cdn.com/v4/letter/a/e9c0ed/32.png) [@A\_Berger](https://rubytalk.org/u/A_Berger)\
**Post date:** [23 June 2016 10:47 UTC](https://rubytalk.org/t/string-method/73675/18 "2016-06-23T10:47:14Z")

</div>

No problem - but why no exception?!  
same with 2048\*11 (just nil)!  
Works with 2048\*10

> **···**
>
> Am 23.06.2016 12:39 schrieb "Sophie Wellow" \<sophie.wellow@gmail.com\>:
> 
> > You're asking for almost a Gig of memory there...
> > 
> > On Thu, Jun 23, 2016 at 11:29 AM, A Berger \<aberger7890@gmail.com\> wrote:
> > 
> > > ...meant @w=2048\*100
> > > 
> > > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);
> > 
> > Unsubscribe: \<mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe\>  
> > \<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt);

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [23 June 2016 14:37 UTC](https://rubytalk.org/t/string-method/73675/19 "2016-06-23T14:37:15Z")

</div>

You do not have to change the encoding of the whole file. For your  
case it should be sufficient to just force the encoding for the  
strings that you use:

irb(main):001:0\> s = "a"  
=\> "a"  
irb(main):002:0\> s.encoding  
=\> #\<Encoding:UTF-8\>  
irb(main):003:0\> s.force\_encoding 'BINARY'  
=\> "a"  
irb(main):004:0\> s.encoding  
=\> #\<Encoding:ASCII-8BIT\>

Alternatively:

irb(main):005:0\> s.force\_encoding Encoding::BINARY  
=\> "a"  
irb(main):006:0\> s.encoding  
=\> #\<Encoding:ASCII-8BIT\>

Btw. can you please bottom post? Thank you!

Kind regards

robert

> **···**
>
> On Thu, Jun 23, 2016 at 1:26 PM, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> 
> > I've only ever had occasional use for the # encoding ... fragment, and I  
> > didn't even realise that BINARY was an option.
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Matthew\_Kerwin](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/matthew_kerwin/32/1845_2.png) [@Matthew\_Kerwin](https://rubytalk.org/u/Matthew_Kerwin)\
**Post date:** [23 June 2016 11:50 UTC](https://rubytalk.org/t/string-method/73675/20 "2016-06-23T11:50:09Z")

</div>

​It's an alias for ASCII-8BIT. Saves a lot of encoding/decoding overhead  
when doing byte-wise (rather than character-wise) operations.​

> **···**
>
> On 23 June 2016 at 21:26, Sophie Wellow \<sophie.wellow@gmail.com\> wrote:
> 
> > ps. same on 2.3.1 - nice.
> > 
> > I've only ever had occasional use for the # encoding ... fragment, and I  
> > didn't even realise that BINARY was an option.
> 
> --  
> &nbsp;&nbsp;Matthew Kerwin  
> &nbsp;&nbsp;[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)
