Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
String#unpack1
# File tmp/rubies/ruby-4.1.0-preview1/pack.rb, line 37
def unpack1(fmt, offset: 0)
Primitive.pack_unpack1(fmt, offset)
end
Like String#unpack with no block, but unpacks and returns only the first extracted object. See Packed Data.
Related: see Converting to Non-String.
Ruby version changes
-
4.1
-
String#unpackandString#unpack1accept new formatsRandrfor unsigned and signed LEB128 encoded integers. [Feature #21785] -
String#unpackandString#unpack1accept a new format^that returns the current offset. Useful when combined with variable width formats like LEB128. [Feature #21796] -
String#unpackandString#unpack1accept new formatsx!and@!to align the current offset to a byte boundary or to the ABI alignment of another directive. [Feature #22185]
-
-
3.1
-
String#unpackandString#unpack1now accept anoffset:keyword argument to start the unpacking after an arbitrary number of bytes have been skipped. Ifoffsetis outside of the string boundsArgumentErroris raised. [Feature #18254]
-