質問

Rubyとを使用してデータ構造を実装しています bindata 宝石。実装する必要があります Choice 価値。 Bindataのドキュメントによると、次のように選択することができます。

class MyData < BinData::Record
  uint8  :type
  choice :data, :selection => :type do
    type key #option 1
    type key #option 2
  end
end

選択にはデフォルトオプションが必要です。

class MyRecord < BinData::Record
    uint8 :type
    choice :mydata, :selection => :type do
            uint32 0
            uint16 1
    end
end

どうすればそれを処理できますか type ではありません 0 また 1 上記のコードで?

役に立ちましたか?

解決 2

さて、私は回避策を見つけました。とにかく、他のオプションも大歓迎です。

class MyRecord < BinData::Record
    uint8 :data_type
    choice :mydata, :selection => :get_choice do
            uint32 1
            uint16 2
            string 255, :read_length => 4
    end

    def get_choice
            choices = [1, 2]
            if choices.include? data_type
                    return data_type
            else
                    return 255
            end
    end
end

他のヒント

Bindata 1.4.1は、これをネイティブに処理します :default

class MyRecord < BinData::Record
  uint8 :data_type
  choice :mydata, :selection => :data_type do
    uint32 1
    uint16 2
    string :default, :read_length => 4
  end
end

おそらくコンストラクターにデフォルトを設定できます...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top