I'm looking for some StringIO-similar class, that allows me to write and read concurrently from different parts of my program.
From one part of the program I want to write (append) characters to the buffer, from another part I want to read them.
The problem with StringIO is the following:
buffer = StringIO.new
buffer.write "Foobar" # Write to the buffer
buffer.rewind # Move the pointer to beginning
buffer.getc #=> F
buffer.getc #=> o
buffer.write("something") # Write more to the buffer
buffer.string #=> Fosomething
buffer.getc #=> nil
buffer.pos #=> 11
Whenever I write to the buffer, it is written to the current position. Aterwards the position is moved to the last written characters.
What I need is a StringBuffer with two seperate positions for reading and writing, instead of only one. Does something like this exist in ruby or do I have to do in on my own?
You should consider using a Queue. If you do not need thread safety, then a simple array might be fine too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With