我正在使用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,该如何处理?
发布于 2011-06-12 15:43:27
我找到了一份工作。无论如何,任何其他选项也是最受欢迎的。
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发布于 2011-06-20 11:47:00
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发布于 2011-06-15 00:37:06
你可以在构造函数中设置一个默认值...
https://stackoverflow.com/questions/6314915
复制相似问题