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#each_char

() { (::String c) -> void } -> self
() -> ::Enumerator[::String, self]
each_char {|c| ... } → self
each_char → enumerator

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.

See also #each_byte #each_codepoint #each #each_line also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0