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)) }

>
> str = (File.open("saved.m").read)

This works, but is more readable as.

str = File.read("saved.m")

> hix = Marshal.load(str) # this appears to be the problem line
> hix.each_key do |key|
>        puts "key : #{key}"
> end
>
> could anyone tell me what I'm doing wrong ?

Assuming the problem is in the "hard" part of the program. :)

When you have a short snippet like this, running the code line-by-line
in irb is often very helpful, since you can see the return values for
each statement and quickly inspect your variables. At least, that's
what works for me.

 -Michael