# StringIO#ungetc returns dupes in Ruby1.9?

**URL:** <https://www.ruby-forum.com/t/stringio-ungetc-returns-dupes-in-ruby1-9/139722>\
**Category:** Ruby\
**Created:** [June 6, 2008, 11:53am UTC](https://www.ruby-forum.com/t/stringio-ungetc-returns-dupes-in-ruby1-9/139722 "2008-06-06T11:53:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [June 6, 2008, 11:53am UTC](https://www.ruby-forum.com/t/stringio-ungetc-returns-dupes-in-ruby1-9/139722/1 "2008-06-06T11:53:29Z")

</div>

First, I apologize if this was a known issue – couldn’t find any  
mention of  
it in English. Seems to happen only in Ruby 1.9, and I’ve only tried  
with the  
version that comes with my Ubuntu repositories:

$ ruby1.9 --version  
ruby 1.9.0 (2007-12-25 revision 14709) [x86\_64-linux]

IO#ungetc is supposed to push one character back onto the IO stream,  
such that  
it will be at the beginning of the next read. I think the idea is to let  
one “peek” at upcoming characters – or at least, that’s how I’m using  
it.

And that works fine, in Ruby 1.8, and on Files, but not on StringIOs in  
Ruby  
1.9:

$ irb1.9  
irb(main):001:0\> require ‘stringio’  
=\> true  
irb(main):002:0\> s = StringIO.new(‘what’)  
=\> #StringIO:0x007f5555306428  
irb(main):003:0\> s.getc  
=\> “w”  
irb(main):004:0\> s.getc  
=\> “h”  
irb(main):005:0\> s.ungetc ‘h’  
=\> nil  
irb(main):006:0\> s.getc  
=\> “h”  
irb(main):007:0\> s.getc  
=\> “h”  
irb(main):008:0\> s.getc  
=\> “a”  
irb(main):009:0\> s.getc  
=\> “t”  
irb(main):010:0\> s.getc  
=\> nil

To clarify: The ‘h’ was read once, so it should have been off the stack,  
so to  
speak. It was then “unread”, and read again… Then again. Even stranger  
things happen with rewind after ungetc.

This is a bit annoying, as it’s the whole reason I was using StringIOs  
in the  
first place.

Can anyone duplicate this with the latest 1.9?
