Skip to content

Bug?: Different Component instances sharing same attribut instance #17

@Dahie

Description

@Dahie

Hi,

I just took 2 hours debugging a weird behavior, I could nail down to different Components instances sharing attribute models.

You can reproduce it like this:

Open an simple Ruby console, require "draco.rb" and run this code:

class A < Draco::Component
  attribute :list, default: []
end

class B < Draco::Entity
  component A
end

class C < Draco::Entity
	component A
end

b1 = B.new
b2 = B.new
c = C.new
puts b1.a.list.object_id
puts b2.a.list.object_id
puts c.a.list.object_id
puts b1.a.list == b2.a.list
puts b1.a.list == c.a.list

the list object in instances b1, b2 and c is the same.
Adding something to the list in b1.a will have it added in c.a

b.a.list << "Example"
#=> ["Example"]
c.a.list
#=> ["Example"]

The same happens on other types, like a Hash.

I'm working around this now by using Entity#after_initialize like this:

class B < Draco::Entity
  component :A

  def after_initialize
    a.list = []
  end
end

Is this expected behavior or a bug? I got caught by surprise.

Regards,
Dahie

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions