ungetc(p1)
public
Pushes back one character (passed as a parameter) onto ios, such
that a subsequent buffered read will return it.
Only one character may be pushed back before a subsequent read operation (that is, you will be able to read only the last of several characters that have
been pushed back). Has no effect with unbuffered reads (such as
IO#sysread).
f = File.new("testfile")
c = f.getc
f.ungetc(c)
f.getc
Show source
/*
* call-seq:
* ios.ungetc(integer) => nil
*
* Pushes back one character (passed as a parameter) onto <em>ios</em>,
* such that a subsequent buffered read will return it. Only one character
* may be pushed back before a subsequent read operation (that is,
* you will be able to read only the last of several characters that have been pushed
* back). Has no effect with unbuffered reads (such as <code>IO
*
* f = File.new("testfile")
* c = f.getc
* f.ungetc(c)
* f.getc
*/
VALUE
rb_io_ungetc(io, c)
VALUE io, c;
{
rb_io_t *fptr;
int cc = NUM2INT(c);
GetOpenFile(io, fptr);
if (!(fptr->mode & FMODE_RBUF))
rb_raise(rb_eIOError, "unread stream");
rb_io_check_readable(fptr);
if (ungetc(cc, fptr->f) == EOF && cc != EOF) {
rb_raise(rb_eIOError, "ungetc failed");
}
return Qnil;
}