After experimenting a bit, I found the following solution works:
require 'bindata'
class NumberArray < BinData::Array
uint16le initial_value: 0
end
class Test < BinData::Record
number_array :field, read_until: :eof
end
obj = Test.new
obj[:field].assign([1, 2, 3, 4, 5])
str = obj.to_binary_s
puts obj.num_bytes # 10
puts obj.inspect # {"field"=>[1, 2, 3, 4, 5]}
puts str.inspect # "\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00"
newobj = Test.read(str)
puts newobj.num_bytes # 10
puts newobj.inspect # {"field"=>[1, 2, 3, 4, 5]}
Note the use of read_until: :eof
when defining the array field. Without this the read operation will only read to the defined value of initial_length
(which is mutually exclusive with read_until
).