Question

Je suis une structure de mise en œuvre de données en utilisant Ruby et BinData joyau . Je dois mettre en œuvre une valeur de Choice. Selon la documentation BinData, un choix peut être mis en œuvre:

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

Je dois avoir une option par défaut dans le choix:

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

Comment peut-il être manipulé si type n'est pas 0 ou 1 dans le code ci-dessus?

Était-ce utile?

La solution 2

Eh bien, je trouve un travail autour. Quoi qu'il en soit toute autre option est également la bienvenue.

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

Autres conseils

BinData 1.4.1 poignées nativement avec :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

vous pouvez définir une valeur par défaut dans un constructeur peut-être ...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top