B
Bill Lear
I'm confused by how YAML seems to not work with ruby exceptions. I
tried searching in vain, thus this quick post.
I would like to serialize and deserialize exception objects, but when I
deserialize a serialized exception, it does not appear to be able to
access the original message, even thought the YAML output seems to have
it. I would think that for any ruby object 'obj', the following should
hold:
obj == YAML::load(YAML::dump(obj))
but this seems to not be the case. Here is a short test script
illustrating the issue:
require 'cgi'
require 'yaml'
begin
raise "Ain't got no mojo"
rescue => ex
if YAML::load(YAML::dump(ex)) != ex
puts "NOT EQUAL"
end
puts "class=#{ex.class} message=#{ex.message}"
puts ex.message
dumped = YAML::dump(ex)
puts dumped
loaded = YAML::load(dumped)
puts "class=#{loaded.class} message=#{loaded.message}"
dumped_2 = YAML::dump(loaded)
puts dumped_2
loaded_2 = YAML::load(dumped_2)
puts "class=#{loaded_2.class} message=#{loaded_2.message}"
end
and here is a run:
% ruby --version
ruby 1.8.6 (2008-03-03 patchlevel 114) [x86_64-linux]
% ruby test_exception_dump.rb
NOT EQUAL
class=RuntimeError message=Ain't got no mojo
Ain't got no mojo
--- !ruby/exception:RuntimeError
message: Ain't got no mojo
class=RuntimeError message=RuntimeError
--- !ruby/exception:RuntimeError
message: RuntimeError
mesg: Ain't got no mojo
class=RuntimeError message=RuntimeError
Have I misunderstood something here, or is there something fundamentally
wrong with ruby YAML?
Bill
tried searching in vain, thus this quick post.
I would like to serialize and deserialize exception objects, but when I
deserialize a serialized exception, it does not appear to be able to
access the original message, even thought the YAML output seems to have
it. I would think that for any ruby object 'obj', the following should
hold:
obj == YAML::load(YAML::dump(obj))
but this seems to not be the case. Here is a short test script
illustrating the issue:
require 'cgi'
require 'yaml'
begin
raise "Ain't got no mojo"
rescue => ex
if YAML::load(YAML::dump(ex)) != ex
puts "NOT EQUAL"
end
puts "class=#{ex.class} message=#{ex.message}"
puts ex.message
dumped = YAML::dump(ex)
puts dumped
loaded = YAML::load(dumped)
puts "class=#{loaded.class} message=#{loaded.message}"
dumped_2 = YAML::dump(loaded)
puts dumped_2
loaded_2 = YAML::load(dumped_2)
puts "class=#{loaded_2.class} message=#{loaded_2.message}"
end
and here is a run:
% ruby --version
ruby 1.8.6 (2008-03-03 patchlevel 114) [x86_64-linux]
% ruby test_exception_dump.rb
NOT EQUAL
class=RuntimeError message=Ain't got no mojo
Ain't got no mojo
--- !ruby/exception:RuntimeError
message: Ain't got no mojo
class=RuntimeError message=RuntimeError
--- !ruby/exception:RuntimeError
message: RuntimeError
mesg: Ain't got no mojo
class=RuntimeError message=RuntimeError
Have I misunderstood something here, or is there something fundamentally
wrong with ruby YAML?
Bill