xanderflood
xanderflood

Reputation: 856

Ruby bidirectional string buffer

Does ruby have a native bidirectional string buffer class? I'd like to be able to do something like this:

buf = Buffer.new

Thread.new do
  while true
    # do some work
    buf << result
  end

  buf.close
end

Thread.new do
  until buf.eof?
    result = buf.readline
    # do some work
  end
end

StringIO supports either reading or writing, but not both. If I initialize a new one and write to it, then try to read from it, I won't get anything. Is there any way to get a plain old communication stream like this that doesn't require using mkfifo or something?

Upvotes: 1

Views: 175

Answers (1)

Artem Ignatiev
Artem Ignatiev

Reputation: 341

StringIO supports either reading or writing, but not both.

You can read from StringIO object you've just written to, just call rewind on your object.

I think you're looking after IO::pipe.

Upvotes: 7

Related Questions