Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

String#bytes

() -> ::Array[::Integer]
() { (::Integer byte) -> void } -> self
bytes → array_of_bytes

Returns an array of the bytes in self:

Example
'hello'.bytes # => [104, 101, 108, 108, 111]
'тест'.bytes  # => [209, 130, 208, 181, 209, 129, 209, 130]
'こんにちは'.bytes
# => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
See also #byterindex #bytesize #byteindex #byteslice also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0