From: Brian Candler Date: 2012-09-13T16:57:04+09:00 Subject: Re: Marshaling classes Unfortunately Marshal.dump doesn't dump even instance variables of a class, just the name of the class. --8<---- class Dog @quantity = 5 def self.change_quantity=(n) @quantity = n end def self.show_quantity @quantity end end Dog.change_quantity = 99 File.open('anyfile.mrs', 'w+') do |f| Marshal.dump(Dog, f) end Dog.change_quantity = 50 Dog2 = Marshal.load(File.open('anyfile.mrs')) puts Dog.show_quantity # 50 puts Dog2.show_quantity # 50 puts Dog.object_id == Dog2.object_id # true --8<---- $ hexdump -C anyfile.mrs 00000000 04 08 63 08 44 6f 67 |..c.Dog| 00000007 So I guess it's up to you to iterate over instance_variables: --8<---- class IVDumper def initialize(klass) @klass = klass @ivs = {} klass.instance_variables.each do |k| @ivs[k] = klass.instance_variable_get(k) end end def restore @ivs.each do |k,v| @klass.instance_variable_set(k,v) end @klass end end def IVDumper(k) IVDumper.new(k) end Dog.change_quantity = 99 File.open('anyfile.mrs', 'w+') do |f| Marshal.dump(IVDumper(Dog), f) end Dog.change_quantity = 50 Marshal.load(File.open('anyfile.mrs')).restore puts Dog.show_quantity # 99 --8<---- You can probably make this more transparent, e.g using _dump/_load methods in IVDumper -- Posted via http://www.ruby-forum.com/.