From: Michael Libby Date: 2008-08-22T00:54:05+09:00 Subject: Re: possible simple marshall error On Thu, Aug 21, 2008 at 10:50 AM, Michael Libby wrote: > On Thu, Aug 21, 2008 at 10:34 AM, Lex Williams wrote: >> I recently tried to learn to use Marshal with a simple script , but I >> keep getting the following exception : x.rb:6:in `load': marshal data >> too short (ArgumentError) >> >> This is the script : >> >> hsh = {:first => [1,2,3],:second => [4,5,6] } >> >> File.open("saved.m","w").puts(Marshal.dump(hsh)) > > This line is actually the problem from what I can tell. > > Try instead: > > File.open("saved.m", "w"){|f| f.puts(Marshal.dump(hsh)) } And allow me to point out why what you have "should" work (and might in some cases), but doesn't: buffering. The way you had it, you open a file, and write to it, but never close the file before trying to read the file. So what you wrote to the file is still in the file buffer rather than written to disk. At least this is how it worked on my system (WindowsXP). I believe this problem would be the same on Linux. Using File::open with a block automatically closes the filehandle at the end of the block, flushing the buffer and giving the later File::read something to actually read. -Michael