Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby StringIO for concurrent reading and writing

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?

like image 695
0xAffe Avatar asked Dec 07 '25 03:12

0xAffe


1 Answers

You should consider using a Queue. If you do not need thread safety, then a simple array might be fine too.

like image 164
David Grayson Avatar answered Dec 08 '25 18:12

David Grayson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!