ruby - 使用 Ruby BinData gem 读取选项

标签 ruby binary-data bindata

我正在使用 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不是01,如何处理?

最佳答案

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/

相关文章:

ruby - 是否有预先构建的函数来添加两个数组中的元素?

ruby - Ruby 中的进程间信号处理

mysql - 固定宽度二进制数据的理想字段类型

ruby - 如何从二进制数据的偏移量开始读取?

r - 如何计算生成相关二元变量的边际概率

ruby - 在 Ruby 中,返回 'enumerable' 是什么意思

ruby - 为什么在重定向时,Ruby 的 STDERR 输出先于 STDOUT 输出?

java - 如何使用 JSP 提供文件?

binary-data - 如何使用 CME 简单二进制编码