ruby / stringio

Pseudo `IO` class from/to `String`.
BSD 2-Clause "Simplified" License
33 stars 26 forks source link

Appending text to initialized StringIO instance #8

Closed throwaway-account-1 closed 4 years ago

throwaway-account-1 commented 4 years ago

Hello,

I'm not sure if this is a bug or a feature mimicking one-way IO, but I got a bit confused with this functionality:

Initializing an empty StringIO instance with no constructor parameters allows me to read-from and write-to it:

a = StringIO.new
a << 'test'
a << 'test'
a.string # => "testtest"

But then when I use the constructor to initialise an initial value it becomes read-only:

b = StringIO.new 'test'
b << 'test'
b.string # => "test"

I would assume this should also result in "testtest" but I might be wrong. It looks like when I create a StringIO instance and initialise it using the constructor it becomes read-only. Is this intentional?

Also I couldn't find any documentation on the << operator in regards to StringIO so maybe I shouldn't be using it?

Sorry about the inconvenience

Extra info:

$ gem list | grep stringio
stringio (default: 0.1.0)
nobu commented 4 years ago

Probably you have frozen-string-literal pragma in the source and pass a frozen string literal. In that case, the StringIO instance is read-only.