From: Ryan Davis Date: 2012-09-13T08:51:19+09:00 Subject: Re: Marshaling classes On Sep 12, 2012, at 16:13 , Damián M. González wrote: > Guys, how are you? > I'm stuck thinking about how can I save the state of a class. I want to > save the class variables of some classes. Do I have to require to > metaprogramming for this? I mean: save those variables in an object, > then dump it marshaling and then re-construct a class from that object > when is loaded. I'll try to explain the situation in an example: Two ways I can think of off of the top of my head: 1) use `maglev-ruby -Mpersistent` to run your code and you're done. > class Dog > @@quantity = 5 > def self.change_quantity=(n) > @@quantity = n > end > def self.show_quantity > @@quantity > end > end 2) Rename your methods to reflect good ruby (def self.quantitiy and def self.quantity=) and then make them persist your values. Change the getter to a lazy accessor that will read in the value from disk (or whatever) and change the setter to persist the change back out. def self.quantity @@quantity ||= Marshal.load(...) # or whatever end