我正在寻找一些StringIO-similar类,它允许我从程序的不同部分并发地写和读。
从程序的一个部分,我想要将字符写(附加)到缓冲区,从另一个部分,我想要读取它们。
StringIO的问题如下:
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每当我写到缓冲区时,它都被写入到当前位置。另外,该位置被移到最后一个手写字符。
我需要的是一个StringBuffer,它有两个独立的读写位置,而不是一个。像这样的东西是存在于红宝石中还是我必须自己去做?
发布于 2016-08-31 15:21:36
您应该考虑使用队列。如果您不需要线程安全,那么一个简单的数组也可以。
发布于 2019-10-29 19:07:02
如果您的程序是单线程的,请尝试coroutines,也就是块。
def do_stuff
yield rand(100)
end
100.times do
do_stuff { |response| puts response }
endhttps://stackoverflow.com/questions/39253219
复制相似问题