Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOC] Fix String#unpack and #unpack1 docs #5331

Merged
merged 1 commit into from Dec 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 15 additions & 14 deletions pack.rb
Expand Up @@ -164,12 +164,6 @@ class String
# platform-independent consistent size. Spaces are ignored in the
# format string.
#
# The keyword <i>offset</i> can be given to start the decoding after skipping
# the specified amount of bytes:
# "abc".unpack("C*") # => [97, 98, 99]
# "abc".unpack("C*", offset: 2) # => [99]
# "abc".unpack("C*", offset: 4) # => offset outside of string (ArgumentError)
#
# See also String#unpack1, Array#pack.
#
# "abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "]
Expand Down Expand Up @@ -267,6 +261,12 @@ class String
# X | --- | skip backward one byte
# x | --- | skip forward one byte
#
# The keyword <i>offset</i> can be given to start the decoding after skipping
# the specified amount of bytes:
# "abc".unpack("C*") # => [97, 98, 99]
# "abc".unpack("C*", offset: 2) # => [99]
# "abc".unpack("C*", offset: 4) # => offset outside of string (ArgumentError)
#
# HISTORY
#
# * J, J! j, and j! are available since Ruby 2.3.
Expand All @@ -283,12 +283,6 @@ def unpack(fmt, offset: 0)
# Decodes <i>str</i> (which may contain binary data) according to the
# format string, returning the first value extracted.
#
# The keyword <i>offset</i> can be given to start the decoding after skipping
# the specified amount of bytes:
# "abc".unpack1("C*") # => 97
# "abc".unpack1("C*", offset: 2) # => 99
# "abc".unpack1("C*", offset: 4) # => offset outside of string (ArgumentError)
#
# See also String#unpack, Array#pack.
#
# Contrast with String#unpack:
Expand All @@ -299,11 +293,18 @@ def unpack(fmt, offset: 0)
# In that case data would be lost but often it's the case that the array
# only holds one value, especially when unpacking binary data. For instance:
#
# "\xff\x00\x00\x00".unpack("l") #=> [255]
# "\xff\x00\x00\x00".unpack1("l") #=> 255
# "\xff\x00\x00\x00".unpack("l") #=> [255]
# "\xff\x00\x00\x00".unpack1("l") #=> 255
#
# Thus unpack1 is convenient, makes clear the intention and signals
# the expected return value to those reading the code.
#
# The keyword <i>offset</i> can be given to start the decoding after skipping
# the specified amount of bytes:
# "abc".unpack1("C*") # => 97
# "abc".unpack1("C*", offset: 2) # => 99
# "abc".unpack1("C*", offset: 4) # => offset outside of string (ArgumentError)
#
def unpack1(fmt, offset: 0)
Primitive.pack_unpack1(fmt, offset)
end
Expand Down