Estoy ejecutando Ruby instalado desde RubyInstaller. Aquí está la versión:¿Por qué arrojar un FormatError al descargar y cargar un Hash usando Marshal en Ruby?
C:\Users\Sathya>ruby -v
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
Aquí está el código exacto, que está lanzando el error:
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
Ahora, tratando de cargarla.
Marshal.load (File.read('timehash'))
da el error:
ArgumentError: dump format error for symbol(0x42)
from (irb):10:in `load'
from (irb):10
from C:/Ruby192/bin/irb:12:in `<main>'
Por qué es esto lanzando un error? ¿Estoy haciendo algo mal, o es esto un error?
estoy corriendo en Windows 7 Ultimate de 64 bits
He aquí los resultados del código de depuración editado habías mencionado:
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
Resultados de EDIT 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>'
Algunas de las características tros no salió, he aquí una muestra:
Ahora estoy un formato de hora de error diferente
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>'
Si vertido a un filestream se permite, ver lo que sucede si se escribe a un objeto StringIO? –