为什么在Ruby中使用Marshal转储和加载Hash会抛出FormatError?

时间:2011-10-04 14:07:32

标签: ruby marshalling

我正在运行从RubyInstaller安装的Ruby。这是版本:

C:\Users\Sathya>ruby -v
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]

这是确切的代码,它引发了错误:

hashtime = Hash.new(Time.mktime('1970'))
hashtime[1]  = Time.now
=> 2011-10-04 19:26:53 +0530
print hashtime
{1=>2011-10-04 19:26:53 +0530}=> nil
hashtime[1]  = Time.now
=> 2011-10-04 19:27:20 +0530
print hashtime
{1=>2011-10-04 19:27:20 +0530}=> nil
File.open('timehash','w') do |f|
  f.write Marshal.dump(hashtime)
end
=> 56  

现在,尝试加载它。

Marshal.load (File.read('timehash'))

给出错误:

ArgumentError: dump format error for symbol(0x42)
        from (irb):10:in `load'
        from (irb):10
        from C:/Ruby192/bin/irb:12:in `<main>'

为什么会抛出错误?我做错了什么,或者这是一个错误?

我在Windows 7旗舰版,64位

上运行

以下是您提到的已编辑调试代码的结果:

hashtime = Hash.new
=> {}
hashtime[1] = Time.now
=> 2011-10-04 20:49:52 +0530
hashdump = Marshal.dump(hashtime)
=> "\x04\b{\x06i\x06Iu:\tTime\r\x8F\xE4\e\x80<\xADGO\x06:\voffseti\x02XM"
hashtime = Marshal.load (hashdump)
=> {1=>2011-10-04 20:49:52 +0530}
print hashtime
{1=>2011-10-04 20:49:52 +0530}=> nil  

编辑结果2:

hashtime = Hash.new
=> {}
hashtime[1] = Time.now
=> 2011-10-04 21:04:24 +0530
hashdump = Marshal.dump(hashtime)
=> "\x04\b{\x06i\x06Iu:\tTime\r\x8F\xE4\e\x80\x92o\x8C\x89\x06:\voffseti\x02XM"
print "hashdump: #{hashdump}"
ÅS?ÇÆoîë?:?offseti?XM=> nile
File.open('timehash','w') do |f|
 f.write hashdump
end
=> 36
hashdump2 = File.read('timehash')
=> "\x04\b{\x06i\x06Iu:\tTime\n\x8F\xE4\e\x80\x92o\x8C\x89\x06:\voffseti\x02XM"
print "hashdump2: #{hashdump2}"
hashdump2:{?i?Iu:       Time
ÅS?ÇÆoîë?:?offseti?XM=> nil
hashtime2 = Marshal.load (hashdump2)
ArgumentError: dump format error for symbol(0x8c)
        from (irb):73:in `load'
        from (irb):73
        from C:/Ruby192/bin/irb:12:in `<main>'  

有些角色没有出来,这是截图:

hash-dump-marshal


现在我得到时间格式不同的错误

hashtime = Hash.new
=> {}
hashtime[1] = Time.now
=> 2011-10-04 21:23:15 +0530
hashdump = Marshal.dump(hashtime)
=> "\x04\b{\x06i\x06Iu:\tTime\r\x8F\xE4\e\x80\xB9\xE1\xFB\xD4\x06:\voffseti\x02X
M"
print "hashdump: #{hashdump}"
ÅΣ←Ç╣ß√╘♠:♂offseti☻XM=> nile
File.open('timehash','wb') do |f|
 f.write hashdump
end
=> 36
hashdump2 = File.read('timehash')
=> "\x04\b{\x06i\x06Iu:\tTime\n\x8F\xE4\e\x80\xB9\xE1\xFB\xD4\x06:\voffseti\x02X
M"
print "hashdump2: #{hashdump2}"
hashdump2:{♠i♠Iu:       Time
ÅΣ←Ç╣ß√╘♠:♂offseti☻XM=> nil
hashtime2 = Marshal.load (hashdump2)
TypeError: marshaled time format differ
        from (irb):10:in `_load'
        from (irb):10:in `load'
        from (irb):10
        from C:/Ruby192/bin/irb:12:in `<main>'

3 个答案:

答案 0 :(得分:12)

您需要通过将b附加到文件模式来以二进制模式写入文件:

File.open('timehash','wb') do |f|
  f.write Marshal.dump(hashtime)
end

你可以通过比较写入磁盘之前的字符串(来自我们的调试)和回读之后来看到这个问题:

=> "\x04\b{\x06i\x06Iu:\tTime\r\x8F\xE4\e\x80\x92o\x8C\x89\x06:\voffseti\x02XM"
=> "\x04\b{\x06i\x06Iu:\tTime\n\x8F\xE4\e\x80\x92o\x8C\x89\x06:\voffseti\x02XM"
                             ^^

\r(回车)正在更改为\n(换行符)

然而,即使使用二进制修饰符,您的系统似乎也没有服从您并且正在将\r更改为\n ...所以让我们尝试将数据编码为base64:

File.open('timehash','w') do |f|
  hashtime_marshal = Marshal.dump(hashtime)
  f.write [hashtime_marshal].pack("m")
end

hashtime_encoded = File.read('timehash')
hashtime = Marshal.load( hashtime_encoded.unpack("m")[0] )

如果有效,请告诉我?


旧信息:

不要将任何内容传递给Hash.new

>> hashtime = Hash.new
=> {}
>> hashtime[1] = Time.now
=> Tue Oct 04 10:57:49 -0400 2011
>> hashtime
=> {1=>Tue Oct 04 10:57:49 -0400 2011}
>> File.open('timehash','w') do |f|
?>   f.write Marshal.dump(hashtime)
>> end
=> 22
>> Marshal.load (File.read('timehash'))
(irb):10: warning: don't put space before argument parentheses
=> {1=>Tue Oct 04 10:57:49 -0400 2011}

文档指出obj Hash.new参数是默认值... 应该正常工作...我不知道为什么它不是......但在您的情况下nil是可接受的默认值,只需检查值是否为nil,如果是,请改为使用Time.mktime('1970')

编辑:这解决了我的问题,但是,我在OS X而不是Windows。所以,让我们尝试一些调试。运行以下代码会发生什么?

hashtime = Hash.new
hashtime[1] = Time.now
hashdump = Marshal.dump(hashtime)
hashtime = Marshal.load (hashdump)
print hashtime

编辑#2:好的。因此Marshal.dumpMarshal.load似乎有效。看起来像文件I / O的东西...请发布以下代码的结果......

hashtime = Hash.new
hashtime[1] = Time.now
hashdump = Marshal.dump(hashtime)
print "hashdump: #{hashdump}"
File.open('timehash','w') do |f|
  f.write hashdump
end
hashdump2 = File.read('timehash')
print "hashdump2: #{hashdump2}"
hashtime2 = Marshal.load (hashdump2)
print hashtime2

答案 1 :(得分:7)

而不是使用File.read阅读,请尝试File.binreadFile.open('timehash', 'rb')

答案 2 :(得分:5)

@Josh和@derp的2个答案的组合对我有用。这是代码(写入文件):

hashtime = Hash.new(Time.mktime('1970'))
hashtime[1]  = Time.now
File.open('timehash','wb') do |f|
  f.write Marshal.dump(hashtime)
end
newhash = Marshal.load (File.binread('timehash'))
p newhash
p newhash.default

结果如下:

c:\apps\ruby>ruby h.rb
{1=>2011-10-05 08:09:43 +0200}
1970-01-01 00:00:00 +0100