# In-memory implementation of IO class

**URL:** <https://www.ruby-forum.com/t/in-memory-implementation-of-io-class/173674>\
**Category:** Ruby\
**Created:** [August 13, 2009, 5:08am UTC](https://www.ruby-forum.com/t/in-memory-implementation-of-io-class/173674 "2009-08-13T05:08:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [August 13, 2009, 5:08am UTC](https://www.ruby-forum.com/t/in-memory-implementation-of-io-class/173674/1 "2009-08-13T05:08:34Z")

</div>

Is there an implementation of the IO class that operates on in-memory  
byte arrays (or other data structure)? I haven’t seen anything in the  
core/stdlib.

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [August 13, 2009, 5:16am UTC](https://www.ruby-forum.com/t/in-memory-implementation-of-io-class/173674/2 "2009-08-13T05:16:46Z")

</div>

On Aug 12, 2009, at 10:07 PM, Nathan B. wrote:

> Is there an implementation of the IO class that operates on in-memory  
> byte arrays (or other data structure)? I haven’t seen anything in the  
> core/stdlib.

Yes, see the standard StringIO library.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [August 13, 2009, 5:34am UTC](https://www.ruby-forum.com/t/in-memory-implementation-of-io-class/173674/3 "2009-08-13T05:34:55Z")

</div>

On Wed, Aug 12, 2009 at 10:15 PM, James G.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Aug 12, 2009, at 10:07 PM, Nathan B. wrote:
> 
> > Is there an implementation of the IO class that operates on in-memory  
> > byte arrays (or other data structure)? I haven’t seen anything in the  
> > core/stdlib.
> 
> Yes, see the standard StringIO library.
> 
> James Edward G. II

Excuse my ignorance – I’m just getting into Ruby, but isn’t the  
StringIO for working with character data? Is there no distinction  
between characters and bytes? I was assuming that StringIO and String  
would cause some form of character encoding/decoding processing to  
kick in.

Thanks

- Nathan

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [August 13, 2009, 6:25am UTC](https://www.ruby-forum.com/t/in-memory-implementation-of-io-class/173674/4 "2009-08-13T06:25:50Z")

</div>

On Aug 12, 11:34 pm, Nathan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Excuse my ignorance – I’m just getting into Ruby, but isn’t the  
> StringIO for working with character data? Is there no distinction  
> between characters and bytes? I was assuming that StringIO and String  
> would cause some form of character encoding/decoding processing to  
> kick in.
> 
> Thanks
> 
> - Nathan

I would think this is what the #bytes, #each\_byte, #readbyte, etc  
methods are for. But I’ve never actually used StringIO _myself_, so I  
can’t say for sure. (However, the fact that Rack and ActionPack both  
use StringIO for processing requests says its safe for raw data)

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [August 13, 2009, 7:23am UTC](https://www.ruby-forum.com/t/in-memory-implementation-of-io-class/173674/5 "2009-08-13T07:23:12Z")

</div>

On Aug 12, 2009, at 20:34, Nathan B. wrote:

> > Yes, see the standard StringIO library.
> > 
> > James Edward G. II
> 
> Excuse my ignorance – I’m just getting into Ruby, but isn’t the  
> StringIO for working with character data? Is there no distinction  
> between characters and bytes? I was assuming that StringIO and String  
> would cause some form of character encoding/decoding processing to  
> kick in.

In ruby 1.8 no. In ruby 1.9 set the encoding to binary.
