如果有多个BinData记录采用以下形式,以下是一些示例:
class DebugInfo < BinData::Record
endian :little
int32 :num
array :data, :type => :debug_log, initial_length: :num
end
class GoalInfo < BinData::Record
endian :little
int32 :num
array :data, :type => :goal, initial_length: :num
end
class PackageInfo < BinData::Record
endian :little
int32 :num
array :data, :type => :package, initial_length: :num
end
所有这些基本相同,只是使用不同类型的对象创建一个数组。有没有办法只是制作其中一个并以某种方式将它传递给我希望它读入数组的对象类型?
答案 0 :(得分:0)
module Info
@base_classes = {}
def self.[](type)
@base_classes[type] ||= Class.new(BinData::Record) {
endian :little
int32 :num
array :data, :type => type, initial_length: :num
}
end
end
然后
class DebugInfo < Info[:debug_log]
end
class GoalInfo < Info[:goal]
end
class PackageInfo < Info[:package]
end
未经测试。
答案 1 :(得分:0)
BinData::Base.read
将除了第一个参数(IO对象)之外的所有参数直接传递给#initialize
,所以我觉得这样的事情会起作用:
class InfoRecord < BinData::Record
endian :little
int32 :num
array :data, type: :array_type, initial_length: :num
attr_reader :array_type
def initialize(*args)
@array_type = args.pop
super
end
end
InfoRecord.read(io, :debug_log)
然而,这对DSL(info_record :my_record, ...
)来说不会很好。
为了解决这个问题,我认为你可以这样做:
class InfoRecord < BinData::Record
# ...
array :data, type: :array_type, initial_length: :num
def array_type
@params[:array_type]
end
end
InfoRecord.read(io, array_type: :debug_log)
我对上述内容不太确定,因为BinData::Base#initialize
处理其Hash参数的方式有点复杂,但希望它允许你这样做。:
class MyRecord < BinData::Record
info_record :my_info, array_type: :debug_log
end