我正在使用 Ruby 和 BinData 实现一个数据结构 gem 。我需要实现一个 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
,如何处理?
最佳答案
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
关于ruby - 使用 Ruby BinData gem 读取选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6314915/