object_id is same between template declaration (&foo) and injection (<<: *foo) #18

Closed
edeustace opened this Issue Jun 19, 2011 · 2 comments

Projects

None yet

2 participants

@edeustace

Hi,
There is a difference of behaviour in the following test script if you load psych. With regular yaml each element has a unique object_id, with psych the object_id of the template an the object into which the template is injected is the same:

=begin
  require 'psych'
rescue LoadError
=end

require 'yaml'

object_ids = []

yaml = YAML::load(<<-eoyml

---
DEFAULTS: &DEFAULTS
  user: ed
one:
  <<: *DEFAULTS
  name: fido 
  user: ed

two:
  name: rover 
  user: bob 
eoyml
)

yaml_value = [yaml]

yaml_value.each do |item|
  item.each do |data|
    data.each do |inner|
      object_ids << inner.object_id
    end
  end
end

b = Hash.new(0)

# iterate over the array, counting duplicate entries
object_ids.each do |v|
  b[v] += 1
end

b.each do |k, v|
  #will raise this error if you require psych
  raise "multiple entries found" if v > 1
end

This is causing a problem with some rails fixtures on a project I'm upgrading.

@tenderlove
Member

This script seems to work fine with the psych gem and with ruby trunk. I think the bug is fixed already:

gem 'psych' # make sure the psych gem is activated
require 'psych'
require 'yaml'

object_ids = []

yaml = YAML::load(<<-eoyml
---
DEFAULTS: &DEFAULTS
  user: ed
one:
  <<: *DEFAULTS
  name: fido 
  user: ed

two:
  name: rover 
  user: bob 
eoyml
)

yaml_value = [yaml]

yaml_value.each do |item|
  item.each do |data|
    data.each do |inner|
      object_ids << inner.object_id
    end
  end
end

b = Hash.new(0)

# iterate over the array, counting duplicate entries
object_ids.each do |v|
  b[v] += 1
end

b.each do |k, v|
  #will raise this error if you require psych
  raise "multiple entries found" if v > 1
end
@tenderlove tenderlove closed this Jun 21, 2011
@edeustace

Super - thanks,
Ed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment