Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
String#unpack
# File tmp/rubies/ruby-4.1.0-preview1/pack.rb, line 25
def unpack(fmt, offset: 0)
Primitive.attr! :use_block
Primitive.pack_unpack(fmt, offset)
end
Extracts data from self to form new objects; see Packed Data.
With a block given, calls the block with each unpacked object.
With no block given, returns an array containing the unpacked objects.
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.3
-
String#unpacknow raisesArgumentErrorfor unknown directives. [Bug #19150]
-
-
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]
-