Hi,
Currently it is very time consuming to leave some instance varibles
out of marshalling. You have to overwrite _dump and _load and write
an appropriate initialize method.
For me this is inacceptable. So why not let Marshal leave out instance
variables that are named in the class instance variables "@transient"?
class A
@transient = :trans_var, :a
attr_reader :trans_var, :a
attr_accessor :b, :c
end
I guess doing this in marshal.c would be only a minor change (array subtraction?),
whereas to do the same in pure Ruby is much harder to accomplish (but it's possible):
class Transient
def _dump(depth)
hash = {}
trans = self.class.class_eval("@transient")
trans = [trans] unless trans.kind_of? Array
trans = trans.collect {|i| "@#{i}"}
(self.instance_variables - trans).each {|var|
hash[var] = self.instance_eval("#{var}")
}
Marshal.dump(hash, depth-1)
end
def self._load(str)
hash = Marshal.load(str)
Thread.critical = true
self.module_eval %{
begin
alias __initialize initialize
rescue NameError
end
def initialize; end
}
obj = self.new
self.module_eval %{
undef initialize
begin
alias initialize __initialize
rescue NameError
end
}
Thread.critical = false
hash.each {|var, val|
obj.instance_eval("#{var} = val")
}
obj
end
end
class B < Transient
@transient = :a
def initialize(a, b)
@a, @b = a, b
end
end
b = B.new("transient!!!", 2)
p b
c = Marshal.load(Marshal.dump(b))
p c
Regards,
Michael
--
Michael Neumann
merlin.zwo InfoDesign GmbH
http://www.merlin-zwo.de