On Thu, Aug 21, 2008 at 10:52 AM, Lex Williams <etaern / yahoo.com> wrote:
> Michael Libby wrote:
>> On Thu, Aug 21, 2008 at 10:34 AM, Lex Williams <etaern / yahoo.com> 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)) }
>>
>
> Thanks Michael , that was the problem indeed . But , what is the
> difference between those two language constructs ?

See my other post in this thread on buffering. What makes it even
harder to understand the bug in your original code is that the File is
open and the output buffered, so your File::read doesn't have anything
to read. But when the program ends the file handle is closed and the
buffer flushed (data written to the file). So when you go to inspect
the actual file there's your data, looking like it would have been
there all along.

-Michael