Frage

Ich implementiere eine Datenstruktur mit Ruby und der Bindata Juwel. Ich muss a implementieren Choice Wert. Gemäß der Bindata -Dokumentation kann eine Auswahl implementiert werden als:

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

Ich muss eine Standardoption in der Auswahl haben:

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

Wie kann das behandelt werden, wenn type ist nicht 0 oder 1 im obigen Code?

War es hilfreich?

Lösung 2

Nun, ich habe eine Arbeit gefunden. Jedenfalls ist auch jede andere Option sehr willkommen.

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

Andere Tipps

Bindata 1.4.1 verarbeitet dies nativ mit :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

Sie könnten vielleicht einen Standard in einem Konstruktor festlegen ...

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top