diff options
author | Victor Shepelev <zverok.offline@gmail.com> | 2021-12-23 22:58:13 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-23 21:58:13 +0100 |
commit | 0b999bef29ead1faeefd7a034dac435795c9fe1a (patch) | |
tree | 1978b934714c46863d200cfc36e9542271d957d5 /pack.rb | |
parent | eba05029b0069ee63ac7e93139d5d4de38c25efb (diff) |
[DOC] Fix String#unpack and #unpack1 docs (#5331)
Notes
Notes:
Merged-By: byroot <jean.boussier@gmail.com>
Diffstat (limited to 'pack.rb')
-rw-r--r-- | pack.rb | 29 |
1 files changed, 15 insertions, 14 deletions
@@ -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 "] @@ -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. @@ -283,12 +283,6 @@ class String # 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: @@ -299,11 +293,18 @@ class String # 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 |