Incompatible Marshal file format error?

Hey all

I am trying to create a script which has two main functions:
  • take a .txt file and convert it into an .rxdata file (using Marshal.dump),
  • take an .rxdata file and convert it into a (readable) .txt file.

This is what I have come up with:
  1. module Rxdata
  2.  
  3.   def self.compile(file)
  4.     @data=[]
  5.     File.open("Data/#{file}.txt",'rb') do |file|
  6.     file.each_line { |line| @data << line.chomp }
  7.   end
  8.     p @data
  9.     dest_file="Data/#{file}.rxdata"
  10.     temp = File.open(dest_file, "w+")
  11.     Marshal.dump(@data, temp)
  12.     temp.close
  13.   end
  14.  
  15.   def self.extract(file)
  16.     @data=Marshal.load("Data/#{file}.rxdata")
  17.     dest_file="Data/#{file}.txt"
  18.     File.new(dest_file)
  19.     temp=File.open(dest_file,"w+")
  20.     for i in @data.size
  21.      temp.write(@data[i])
  22.     end
  23.     temp.close
  24.   end
  25.  
  26. end
Expand to see the code.


The compile method seems to work properly: I insert a .txt file which contains my text in the directory, and the scripts creates a serialized .rxdata file of that. But when I use the extract method (from .rxdata to .txt) it throws me an error which makes me think I am making some kind of mistake somewhere:
error wrote:
Script 'rxdata' line 17: TypeError occured.
incompatible marshal file format (can't be read)
format version 4.8 required; 68.97 given


I would appreciate it if someone would help me out here.
Thanks in advance!
I don't think you can just write raw data to a text file. And if you can its not going to be readable in human english. Its still going to contain all these weird characters and whatnot.

Look into this. http://www.ruby-doc.org/core/classes/Ob ... ml#M001027 Using instance_variables will return all defined instance variables in a class. Using this may be able to help you. And to get the class name use class.name.
Example
  1. class MyClass
  2.   def name
  3.     return self.class.name
  4.   end
  5. end
Expand to see the code.
try writing a inspect type function to a string, and then write to the txt. I think that in ruby it must exist.
I know the data from the file should be an array containing strings, so writing data to the file shouldn't be a problem. But it throws the error on line 17, so the data isn't even loaded yet. The strange thing is that the script can't read the marshaled data it produced itself :huh:

edit: I googled around a bit, and found what the problem is:
The Internet wrote:
*"Incompatible marshal file format (can't read)
> format version 4.8 required: 84.104 given"*
That error message means that it's trying to deserialize a value that
was not serialized in the first place. If the value had been serialized
(aka "marshaled"), then the first two bytes would be the format version.
In your value, the first two bytes are 84 and 104, which are the
letters "Th".

So I guess there's something wrong with the serializing.

edit2: I read that Marshal.dump will throw a TypeError (the error i got) when an object can't be serialized. The variable I am trying to dump is this:
  1. p @data #=> ["Test","Test","Test"]
Expand to see the code.

It seems impossible to serialize an array containing strings, although the Marshal.dump doesn't throw the error, but Marshal.load :huh:
Have you tried using the load_data method?

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=74694