Damin M. Gonzlez ڧѧ 16.09.2012 02:49:
> I'm trying to find out if is possible to serialize a class object, 
> with
> their own instance variables, something like this:
>
> class Foo
>   @x = 10
>   @y = 20
> end
>
>  Then I went to Dave Thomas book: Programming Ruby 1.9 to the section
> which talk about Marshal. It say literaly:
>
>  "Some objects cannot be dumped: if the objects to be dumped include
> bindings, procedure or method objects, instances of class IO, or
> singleton objects, or if you try to dump anonymous classes or 
> modules, a
> TypeError will be raised."
>
>  So obvious Foo is not a singleton object, is a class object. I've 
> tried
> to Marshal this class in the common way and succed, no TypeError 
> arised.
> But when I restore, no clue about his own instance variables. I 
> wonder
> what does mean Dave when he say "anonymous classes or modules"...
>  I've opened a topic short time ago related with this, asking about
> marshaling classes and his class variables, I surrendered trying to 
> save
> classes with his class variables but what about his own instance
> variables, It is not a common object? I just can't? What is the 
> option
> to serialize this kind of data? Do I have to appeal to 
> metaprogramming?
> Thank you guys.

Ruby does not support serializing code. At all. If you serialize 
something
that includes methods, these methods will not be written to disk.

In other words, you cannot meaningfully serialize classes. When you 
serialize
a Class instance, only its name gets written. See:
irb > class Klass; @x = 0; end
  => 0
irb > Marshal.dump Klass
  => "\x04\bc\nKlass"
irb > Object.send :remove_const, :Klass
  => A
irb > Klass = 0
  => 0
irb > Marshal.load "\x04\bc\nKlass"
TypeError: Klass does not refer to class/module
         from (irb):10:in `load'
         from (irb):10
         from /home/whitequark/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in 
`<main>'

Also, as the name is used to find a class, you cannot meaningfully 
serialize
instances of anonymous classes either. see:
irb > k = Class.new
  => #<Class:0x0000000085ea80>
irb > Marshal.dump k.new
TypeError: can't dump anonymous class #<Class:0x0000000085ea80>
         from (irb):13:in `dump'
         from (irb):13
         from /home/whitequark/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in 
`<main>'

I'm not sure as what was mentioned in the book as I don't have it, but 
I guess
that it was only talking about _instances_ of anonymous classes. You 
don't often
need to serialize classes themselves, and, as I've mentioned, you 
cannot serialize
code, be that closures or methods. Objects with singleton methods will 
have those
singleton methods lost:
irb > obj = Object.new
  => #<Object:0x000000008833a8>
irb > def obj.bark
irb >   puts "Howl!"
irb >   end
  => nil
irb > serial = Marshal.dump(obj)
TypeError: singleton can't be dumped
         from (irb):18:in `dump'
         from (irb):18
         from /home/whitequark/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in 
`<main>'


-- 
   WBR, Peter Zotov.