Skip to content

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

instance method · added in Ruby 3.4

String#append_as_bytes

(::String) -> ::String
append_as_bytes(*objects) → string

Concatenates each object in objects into self without any encoding validation or conversion and returns self:

Example
s = 'foo'
s.append_as_bytes(" \xE2\x82")  # => "foo \xE2\x82"
s.valid_encoding?               # => false
s.append_as_bytes("\xAC 12")
s.valid_encoding?               # => true

For each given object object that is an Integer, the value is considered a Byte. If the Integer is bigger than one byte, only the lower byte is considered, similar to String#setbyte:

Example
s = ""
s.append_as_bytes(0, 257)             # =>  "\u0000\u0001"

Related: String#<<, String#concat, which do an encoding aware concatenation.

Ruby version changes

  1. 3.4
    • String#append_as_bytes was added to more easily and efficiently work with binary buffers and protocols. It directly concatenate the arguments into the string without any encoding validation or conversion. [Feature #20594]

See also #[]= #ascii_only? #[] #b also in 4.1 · 4.0