Closed spurnaye closed 7 years ago
I would have expected it, but maybe only because other solutions I know (such as the has_defaults
gem) work the same way. If we find a good way to avoid this issue without breaking things, we should probably change it.
The manual fix for now is to just use a proc, such as default: -> { { key1: 123, key2: 'xyz' } }
.
Theoretically, we could try to dup
any default values, but I'm not totally sure about it. Users might have come to rely on the fact that a single instance is shared (maybe not in the example of a hash, but perhaps for other kinds of objects), and there are probably objects out there that don't implement dup
properly.
Maybe just do it for arrays and hashes?
Thanks for the reply! Following code has ArgumentError
attribute :hash_attribute, :hash, default: -> { {key1: 123, key2: 'xyz'} }
Right, need to use a proc instead:
attribute :hash_attribute, :hash, default: proc { {key1: 123, key2: 'xyz'} }
@kratob working find that way. Closing for now, but I still think those hashes should be separate objects without use of proc.
I came across this issue when fixing some specs. Following points came out of my observation: Ruby 2.2.1/2.3.1 My code:
is this expected?