summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/psych/visitors/to_ruby.rb19
-rw-r--r--test/visitors/test_to_ruby.rb8
2 files changed, 24 insertions, 3 deletions
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index 2f6c3b6..2f94be8 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -86,9 +86,22 @@ module Psych
h = Hash[*o.children.map { |c| accept c }].to_a
if klassname && klassname.length > 1
- s = klassname.sub(/^:/,'').split('::').inject(Object) { |k,sub|
- k.const_get sub
- }.allocate
+ name = klassname.sub(/^:/, '')
+ s = nil
+ retried = false
+
+ begin
+ s = name.split('::').inject(Object) { |k,sub|
+ k.const_get sub
+ }.allocate
+ rescue NameError => ex
+ name = "Struct::#{name}"
+ unless retried
+ retried = true
+ retry
+ end
+ raise ex
+ end
h.each { |k,v| s.send("#{k}=", v) }
s
else
diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb
index f992752..677b8f5 100644
--- a/test/visitors/test_to_ruby.rb
+++ b/test/visitors/test_to_ruby.rb
@@ -11,6 +11,14 @@ module Psych
@visitor = ToRuby.new
end
+ def test_legacy_struct
+ foo = Struct.new('Foo', :bar)
+ assert_equal foo.new('baz'), Psych.load(<<-eoyml)
+!ruby/struct:Foo
+ bar: baz
+ eoyml
+ end
+
def test_binary
gif = "GIF89a\f\x00\f\x00\x84\x00\x00\xFF\xFF\xF7\xF5\xF5\xEE\xE9\xE9\xE5fff\x00\x00\x00\xE7\xE7\xE7^^^\xF3\xF3\xED\x8E\x8E\x8E\xE0\xE0\xE0\x9F\x9F\x9F\x93\x93\x93\xA7\xA7\xA7\x9E\x9E\x9Eiiiccc\xA3\xA3\xA3\x84\x84\x84\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9\xFF\xFE\xF9!\xFE\x0EMade with GIMP\x00,\x00\x00\x00\x00\f\x00\f\x00\x00\x05, \x8E\x810\x9E\xE3@\x14\xE8i\x10\xC4\xD1\x8A\b\x1C\xCF\x80M$z\xEF\xFF0\x85p\xB8\xB01f\r\e\xCE\x01\xC3\x01\x1E\x10' \x82\n\x01\x00;"