Project

General

Profile

Actions

Feature #12752

closed

Unpacking a value from a binary requires additional '.first'

Added by tagomoris (Satoshi Tagomori) over 7 years ago. Updated over 7 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:77249]

Description

When I parsed a binary protocol (msgpack) by String#unpack, there are many similar expressions, like below:

str = io.read(1)
length = str.unpack('C').first
case length
when 4
  data = io.read(4).unpack('N').first
# ...

I know that unpack returns "1 or more" values, but I think there are too many cases to return just one object.
Creating Array objects for return values requires additional cost to create Array object, and calling .first.

I'm happy if I can write the code above like this:

str = io.read(1)
length = str.unpack('C', 0) # index, or str.unpack('C', index: 0)
case length
when 4
  data = io.read(4).unpack('N', 0)
# ...
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0