thanks for your time!
I get a class like this
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
And there's will be an array to store the instance of Vuser Class. Let's say the array's name is vuser_ary.
I want to store this array ( vuser_ary ) to a binary file and I think it's called serialization. I google it and find Marshal in the standard library can do this. Here's an example how I do this according to the example on the Internet :
#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)}
But when I check the size of some.file. I find it's just four bytes. Then I realize that the data stored in some.file may be the reference but not the value of the vuser_ary.
Then my question is how do I store the value of vuser_ary to a binary file. How do I change my code to achieve that? Thanks in advance!
BTW: the value stored in vuser_ary will be like this :
RO_3.2_S4_CommericalRealEstate1_274.log
{1=>"Fail", 2=>"Fail", 3=>"Pass", 4=>"Pass", 5=>"Fail"}
RO_3.2_S4_CommericalRealEstate1_275.log
{11=>"Fail", 2=>"Fail", 3=>"Fail", 4=>"Pass", 5=>"Fail"
RO_3.2_S4_CommericalRealEstate1_276.log
{1=>"Fail", 2=>"Fail", 3=>"Pass", 4=>"Pass", 5=>"Fail"}
RO_3.2_S4_CommericalRealEstate1_277.log
{1=>"Fail", 2=>"Fail", 3=>"Pass", 4=>"Pass", 5=>"Fail"}
RO_3.2_S4_CommericalRealEstate1_278.log
{1=>"Fail", 2=>"Fail", 3=>"Pass", 4=>"Pass", 5=>"Fail"}
RO_3.2_S4_CommericalRealEstate1_279.log
{1=>"Fail", 2=>"Fail", 3=>"Pass", 4=>"Pass", 5=>"Fail"}
RO_3.2_S4_CommericalRealEstate1_280.log
{1=>"Fail", 2=>"Fail", 3=>"Fail", 4=>"Pass", 5=>"Fail"}
Four bytes? It so happens that marshalling the empty array [] produces a 4-byte string on my Ruby:
> Marshal.dump([]).length
=> 4
Are you sure vuser_ary isn't empty when you try marshalling it?
As it happens, there's no difference between a "reference" to an object and the object itself: there are no pointers in Ruby, so if you have an array (and it's non-empty), then it'll get marshalled:
> Marshal.dump([1, 2, 3]).length
=> 10
For good measure:
> vuser_ary = [{1=>"Fail", 2=>"Fail", 3=>"Pass", 4=>"Pass", 5=>"Fail"}]
=> [{1=>"Fail", 2=>"Fail", 3=>"Pass", 4=>"Pass", 5=>"Fail"}]
> Marshal.dump(vuser_ary).length
=> 72
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With