, remove_singleton_class_information!, singleton.
. , .
class Foo
def break_marshal
singleton_class.class_eval do
@@x = true
@x = false
define_method :bar do
puts @@x
puts singleton_class.instance_variable_get(:@x)
puts singleton_class.send(:baz)
puts singleton_class.const_get(:X)
end
define_singleton_method :baz do
"Singleton method in a singleton"
end
end
end
def fix_marshal
singleton_methods.each { |m| singleton_class.send(:remove_method, m) }
singleton_class.class_eval do
instance_variables.each { |v| remove_instance_variable v }
end
end
end
f = Foo.new
f.break_marshal
f.singleton_class.const_set(:X, 1)
f.bar
f.fix_marshal
Marshal.dump f
, . singleton , , , singleton . , singleton .
However , I do not think this is the right way to marshal such an object. Answer @mdesantis to define your own marshal_dumpand marshal_loadmore correct.
source
share