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_byte

() { (::Integer byte) -> void } -> self
() -> ::Enumerator[::Integer, self]
each_byte {|byte| ... } → self
each_byte → enumerator

Calls the given block with each byte (0..255) in the stream; returns self. See Byte IO.

Example
File.read('t.ja') # => "こんにちは"
f = File.new('t.ja')
a = []
f.each_byte {|b| a << b }
a # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
f.close

Returns an Enumerator if no block is given.

Related: IO#each_char, IO#each_codepoint.

See also #each #each_char #cursor_up #each_codepoint also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0