Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump ivars befor hash elements #384

Merged
merged 2 commits into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/psych/visitors/yaml_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,6 @@ def visit_hash_subclass o
node = @emitter.start_mapping(nil, tag, false, Psych::Nodes::Mapping::BLOCK)
register(o, node)

# Dump the elements
accept 'elements'
@emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
o.each do |k,v|
accept k
accept v
end
@emitter.end_mapping

# Dump the ivars
accept 'ivars'
@emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
Expand All @@ -446,6 +437,15 @@ def visit_hash_subclass o
end
@emitter.end_mapping

# Dump the elements
accept 'elements'
@emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
o.each do |k,v|
accept k
accept v
end
@emitter.end_mapping

@emitter.end_mapping
else
tag = "!ruby/hash:#{o.class}"
Expand Down
20 changes: 20 additions & 0 deletions test/psych/test_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ class TestHash < TestCase
class X < Hash
end

class HashWithIvar < Hash
def initialize
@keys = []
super
end

def []=(k, v)
@keys << k
super(k, v)
end
end

class HashWithCustomInit < Hash
attr_reader :obj
def initialize(obj)
Expand All @@ -24,6 +36,14 @@ def setup
@hash = { :a => 'b' }
end

def test_hash_with_ivar
t1 = HashWithIvar.new
t1[:foo] = :bar
t2 = Psych.load(Psych.dump(t1))
assert_equal t1, t2
assert_cycle t1
end

def test_referenced_hash_with_ivar
a = [1,2,3,4,5]
t1 = [HashWithCustomInit.new(a)]
Expand Down