Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
IO#each_char
() { (::String c) -> void } -> self
() -> ::Enumerator[::String, self]
each_char {|c| ... } → self
each_char → enumerator
io.c
View on GitHub ↗
static VALUE
rb_io_each_char(VALUE io)
{
rb_io_t *fptr;
rb_encoding *enc;
VALUE c;
RETURN_ENUMERATOR(io, 0, 0);
GetOpenFile(io, fptr);
rb_io_check_char_readable(fptr);
enc = io_input_encoding(fptr);
READ_CHECK(fptr);
while (!NIL_P(c = io_getc(fptr, enc))) {
rb_yield(c);
}
return io;
}
Calls the given block with each character in the stream; returns self. See Character IO.
Example
File.read('t.ja') # => "こんにちは" f = File.new('t.ja') a = [] f.each_char {|c| a << c.ord } a # => [12371, 12435, 12395, 12385, 12399] f.close
Returns an Enumerator if no block is given.
Related: IO#each_byte, IO#each_codepoint.