I'm using Ruby and trying to read binary data from a TCP interface. The message received contains a header and a payload. The payload is determined by an id from the header.

This is an example:

```
class TCPmessage < BinData:: Record

  class PayloadType_1 < BinData::Record
    uint8 :payloadType_1
    # more payload data
  end

  class PayloadType_2 < BinData::Record
    uint8 :payloadType_2
    # more payload data
  end

  uint8 :payload_id

  array :payload, :type => <<Here I need to select "PayloadType_1" or "PayloadType_2" based on the "payload_id" from above>>, ...

end

```

I tried a few variants but only came up with the following solution:

```
class TCPmessage < BinData:: Record

  class PayloadType_1 < BinData::Record
    uint8 :payload_id
    uint8 :payloadType_1
    # more payload data
  end

  class PayloadType_2 < BinData::Record
    uint8 :payload_id
    uint8 :payloadType_2
    # more payload data
  end

  uint8 :payload_id
end

```

In the main program I first read `payload_id` and then use a `case` statement to choose which class to instantiate next:

```
x = TCPmessage.new
case x.read("TCPmessage").payload_id.to_s
when "1"
  y = TCPmessage::PayloadType_1.new
when "2"
  y = TCPmessage::PayloadType_2.new
end
y.read("TCPmessage")

```

I'm sure there is another solution using the compound types (arrays/choices) from BinData gem but I could not see it.