# Serializing Class and Module objects

**URL:** <https://www.ruby-forum.com/t/serializing-class-and-module-objects/100769>\
**Category:** Ruby\
**Created:** [February 3, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/serializing-class-and-module-objects/100769 "2006-02-03T21:12:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [February 3, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/serializing-class-and-module-objects/100769/1 "2006-02-03T21:12:50Z")

</div>

Hey, \_why, what do you think about allowing YAML to dump/load Class and  
Module objects rather than gagging? I’ve posted code like the following  
(different versions for 1.8.2 and 1.8.4) several times in response to  
inquiries on ruby-talk, and found it useful myself.

=====================================================  
Test code:

enum\_y = [Enumerable, Comparable, String, File].to\_yaml  
puts enum\_y  
p YAML.load(enum\_y)

=====================================================  
Test output:

* * *

- !ruby/module Enumerable
- !ruby/module Comparable
- !ruby/class String
- !ruby/class File  
[Enumerable, Comparable, String, File]

=====================================================  
require ‘yaml’

if defined?(YAML.type\_tag) # old version of YAML

class Module  
def is\_complex\_yaml?  
false  
end  
def to\_yaml( opts = {} )  
YAML::quick\_emit( nil, opts ) { |out|  
out \<\< "!ruby/module "  
self.name.to\_yaml( :Emitter =\> out )  
}  
end  
end  
YAML.add\_ruby\_type(/^module/) do |type, val|  
subtype, subclass = YAML.read\_type\_class(type, Module)  
val.split(/::/).inject(Object) { |p, n| p.const\_get(n)}  
end

class Class  
def to\_yaml( opts = {} )  
YAML::quick\_emit( nil, opts ) { |out|  
out \<\< "!ruby/class "  
self.name.to\_yaml( :Emitter =\> out )  
}  
end  
end  
YAML.add\_ruby\_type(/^class/) do |type, val|  
subtype, subclass = YAML.read\_type\_class(type, Class)  
val.split(/::/).inject(Object) { |p, n| p.const\_get(n)}  
end

else

class Module  
yaml\_as “tag:ruby.yaml.org,2002:module”

```
def Module.yaml_new( klass, tag, val )
  if String === val
    val.split(/::/).inject(Object) {|m, n| m.const_get(n)}
  else
    raise YAML::TypeError, "Invalid Module: " + val.inspect
  end
end

def to_yaml( opts = {} )
  YAML::quick_emit( nil, opts ) { |out|
    out.scalar( "tag:ruby.yaml.org,2002:module", self.name, :plain )
  }
end

```

end

class Class  
yaml\_as “tag:ruby.yaml.org,2002:class”

```
def Class.yaml_new( klass, tag, val )
  if String === val
    val.split(/::/).inject(Object) {|m, n| m.const_get(n)}
  else
    raise YAML::TypeError, "Invalid Class: " + val.inspect
  end
end

def to_yaml( opts = {} )
  YAML::quick_emit( nil, opts ) { |out|
    out.scalar( "tag:ruby.yaml.org,2002:class", self.name, :plain )
  }
end

```

end

end

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [February 3, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/serializing-class-and-module-objects/100769/2 "2006-02-03T21:25:15Z")

</div>

Joel VanderWerf wrote:

> Hey, \_why, what do you think about allowing YAML to dump/load Class and  
> Module objects rather than gagging? I’ve posted code like the following  
> (different versions for 1.8.2 and 1.8.4) several times in response to  
> inquiries on ruby-talk, and found it useful myself.

I’ve been reluctant, because reloading these objects causes trouble if  
you haven’t required the right libraries. But I think if YAML’s error  
message was okay, it would work.

Oh and the best place for getting Syck requests through is now here:  
[http://code.whytheluckystiff.net/syck/newticket](http://code.whytheluckystiff.net/syck/newticket)

\_why

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [February 3, 2006, 9:34pm UTC](https://www.ruby-forum.com/t/serializing-class-and-module-objects/100769/3 "2006-02-03T21:34:24Z")

</div>

why the lucky stiff wrote:

> message was okay, it would work.  
> I thought about rescuing and re-raising the exception as a different  
> exception class, but maybe it is better to leave it as it is:

NameError: uninitialized constant Foo

This is the same exception you would get if you loaded a script file.

The way Marshal does it (in the case of a dumped class that is loaded  
without having the class definition required) is:

ArgumentError: undefined class/module Foo

Maybe that’s how YAML should do it for consistency?

I’d be happy with either way.

> Oh and the best place for getting Syck requests through is now here:  
> [http://code.whytheluckystiff.net/syck/newticket](http://code.whytheluckystiff.net/syck/newticket)

Thanks!

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [February 11, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/serializing-class-and-module-objects/100769/4 "2006-02-11T20:52:16Z")

</div>

why the lucky stiff wrote:

> message was okay, it would work.
> 
> Oh and the best place for getting Syck requests through is now here:  
> [http://code.whytheluckystiff.net/syck/newticket](http://code.whytheluckystiff.net/syck/newticket)

Ok. I’ve submitted this patch and also the :SortKeys issue to the Syck  
Trak.
