首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Ruby BinData gem读取选择

使用Ruby BinData gem读取选择
EN

Stack Overflow用户
提问于 2011-06-11 16:21:57
回答 3查看 1.3K关注 0票数 1

我正在使用Ruby和BinData gem实现一个数据结构。我需要实现一个Choice值。根据BinData文档,可以将选择实现为:

代码语言:javascript
复制
class MyData < BinData::Record
  uint8  :type
  choice :data, :selection => :type do
    type key #option 1
    type key #option 2
  end
end

我需要在选择中有一个默认选项:

代码语言:javascript
复制
class MyRecord < BinData::Record
    uint8 :type
    choice :mydata, :selection => :type do
            uint32 0
            uint16 1
    end
end

如果上述代码中的type不是01,该如何处理?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-12 15:43:27

我找到了一份工作。无论如何,任何其他选项也是最受欢迎的。

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2011-06-20 11:47:00

BinData 1.4.1通过:default本机处理此问题

代码语言:javascript
复制
class MyRecord < BinData::Record
  uint8 :data_type
  choice :mydata, :selection => :data_type do
    uint32 1
    uint16 2
    string :default, :read_length => 4
  end
end
票数 5
EN

Stack Overflow用户

发布于 2011-06-15 00:37:06

你可以在构造函数中设置一个默认值...

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6314915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档