Method: StringIO#ungetc

Defined in:
ext/stringio/stringio.c

#ungetc(character) ⇒ nil

Pushes back (“unshifts”) a character or integer onto the stream; see Character IO.

Returns:

  • (nil)


929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
# File 'ext/stringio/stringio.c', line 929

static VALUE
strio_ungetc(VALUE self, VALUE c)
{
    struct StringIO *ptr = readable(self);
    rb_encoding *enc, *enc2;

    check_modifiable(ptr);
    if (NIL_P(c)) return Qnil;
    if (RB_INTEGER_TYPE_P(c)) {
  int len, cc = NUM2INT(c);
  char buf[16];

  enc = rb_enc_get(ptr->string);
  len = rb_enc_codelen(cc, enc);
  if (len <= 0) rb_enc_uint_chr(cc, enc);
  rb_enc_mbcput(cc, buf, enc);
  return strio_unget_bytes(ptr, buf, len);
    }
    else {
  SafeStringValue(c);
  enc = rb_enc_get(ptr->string);
  enc2 = rb_enc_get(c);
  if (enc != enc2 && enc != rb_ascii8bit_encoding()) {
      c = rb_str_conv_enc(c, enc2, enc);
  }
  strio_unget_bytes(ptr, RSTRING_PTR(c), RSTRING_LEN(c));
  RB_GC_GUARD(c);
  return Qnil;
    }
}