Suppose there's the following class
# derp.rb
class Derp < Struct.new :id
end
When I load "./derp.rb"
twice the program fails with TypeError: superclass mismatch for class Derp
. Ok, this could be managed with require
. But how can I reload such classes for each test run with Spork? require
obviously won't work cause it caches the loaded files.
For those finding this on Google, this is what solved it for me:
You can make sure the struct class is created only once.
Test1 < $test1 ||= Struct.new(:id)
Struct.new
is creating new class for your every load.You can save your
Struct.new
returnedclass
to a variable and you can use that will be always the sameclass
.or You can use
Struct.new
block style instead ofclass
keyword it will only givewarning: already initialized constant Test3
when you reload your file.