Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

IO#readchar

() -> ::String
readchar → string

Reads and returns the next 1-character string from the stream; raises EOFError if already at end-of-stream. See Character IO.

Example
f = File.open('t.txt')
f.readchar     # => "F"
f.close
File.read('t.ja') # => "こんにちは"
f = File.open('t.ja')
f.readchar.ord # => 12371
f.close

Related: IO#getc (will not raise EOFError).

See also #readbyte #readline #read #readlines also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0