Yazid Erman Yazid Erman - 3 months ago 14
Ruby Question

Marshal class, is there a way to find whether a data is already serialized or not?

I am using

Marshal
class to serialize a Ruby object, using the functions:
dump()
and
load()
everything works well, but when a value not related to any serialized data is passed, the
load()
function returns the expected and logical error:

incompatible marshal file format (can't be read)
format version 4.8 required; 45.45 given


What I need is to check if this data had already been serialized or not before loading it. My goal is to avoid this error and do something else.

Answer

Maybe just rescue from the error?

begin
  Marshal.load("foobar")
rescue TypeError
  # not a marshalled object, do something else
  puts "warning: could not load ..."
end