Bug #2449: StringIO#ungetc behavior is contrary to its spec
http://redmine.ruby-lang.org/issues/show/2449

Author: Vladimir Sizikov
Status: Open, Priority: Normal
ruby -v: ruby 1.8.7 (2009-06-12 patchlevel 174) [i386-mswin32]

The spec for StringIO#ungetc() states: "Pushing back behind the beginning of the buffer string is not possible. Nothing will be done if such an attempt is made.".

But: ruby -rstringio -e "io = StringIO.new(''); io.ungetc(?1); io.ungetc(?2); p io.gets"

will print "21" instead of nil. Please also note that rbx and JRuby correctly return nil. Also, there is a stringio test that actually enforses the wrong behavior (test_ungetc), but with the comment: bug? :)

Here's how different MRI versions behave: Looks like MRI 1.8.6 is the correct one:

Z:\work\ruby18-dev.git>pik ruby -rstringio -e "io=StringIO.new('');io.ungetc(?1);io.ungetc(?2); p io.gets"
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

nil

ruby 1.8.6 (2009-08-04 patchlevel 383) [i386-mingw32]

"21"

ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]

nil

ruby 1.8.7 (2009-06-12 patchlevel 174) [i386-mswin32]

"21"

ruby 1.9.0 (2008-06-20 revision 17482) [i386-mswin32]

"21"

ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-mswin32]

"21"

ruby 1.9.1p0 (2009-01-30 revision 21907) [x64-mswin64_80]

"21"

ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-mswin32]

"21"

ruby 1.9.2dev (2009-07-18) [i386-mswin32]

"21"

ruby 1.9.2dev (2009-11-12 trunk 25723) [i386-mingw32]

"21"


----------------------------------------
http://redmine.ruby-lang.org