【发布时间】:2012-07-10 11:53:33
【问题描述】:
感谢您的宝贵时间!
我有这样的课程
class Vuser
def initialize (logfile_name, iteration_hash)
@logfile_name = logfile_name
@iteration_hash = iteration_hash
end
attr_accessor :logfile_name, :iteration_hash
def output_iteration_info ()
puts @logfile_name
puts @iteration_hash
end
end
还有一个数组来存储 Vuser 类的实例。假设数组的名称是 vuser_ary。
我想将此数组 ( vuser_ary ) 存储到一个二进制文件中,我认为这称为序列化。我用谷歌搜索,发现标准库中的 Marshal 可以做到这一点。 这是我根据互联网上的示例如何执行此操作的示例:
#serialization
File.open("some.file","wb") do |file|
Marshal.dump(vuser_ary,file)
end
#loading
vuser_ary = nil
File.open("some.file","rb") {|f| vuser_ary = Marshal.load(f)}
但是当我检查 some.file 的大小时。我发现它只有四个字节。然后我意识到some.file中存储的数据可能是引用而不是vuser_ary的值。
那么我的问题是如何将 vuser_ary 的值存储到二进制文件中。如何更改我的代码以实现这一目标?提前致谢!
顺便说一句:存储在 vuser_ary 中的值将是这样的:
RO_3.2_S4_CommericalRealEstate1_274.log
{1=>“失败”、2=>“失败”、3=>“通过”、4=>“通过”、5=>“失败”}
RO_3.2_S4_CommericalRealEstate1_275.log
{11=>“失败”、2=>“失败”、3=>“失败”、4=>“通过”、5=>“失败”
RO_3.2_S4_CommericalRealEstate1_276.log
{1=>“失败”、2=>“失败”、3=>“通过”、4=>“通过”、5=>“失败”}
RO_3.2_S4_CommericalRealEstate1_277.log
{1=>“失败”、2=>“失败”、3=>“通过”、4=>“通过”、5=>“失败”}
RO_3.2_S4_CommericalRealEstate1_278.log
{1=>“失败”、2=>“失败”、3=>“通过”、4=>“通过”、5=>“失败”}
RO_3.2_S4_CommericalRealEstate1_279.log
{1=>“失败”、2=>“失败”、3=>“通过”、4=>“通过”、5=>“失败”}
RO_3.2_S4_CommericalRealEstate1_280.log
{1=>“失败”、2=>“失败”、3=>“失败”、4=>“通过”、5=>“失败”}
【问题讨论】:
标签: ruby marshalling