On Sep 12, 2012, at 16:13 , Dami=E1n M. Gonz=E1lez =
<lists / ruby-forum.com> 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 =3D 5
>   def self.change_quantity=3D(n)
>     @@quantity =3D n
>   end
>   def self.show_quantity
>     @@quantity
>   end
> end


2) Rename your methods to reflect good ruby (def self.quantitiy and def =
self.quantity=3D) 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 ||=3D Marshal.load(...) # or whatever
end