Creating classes with from YAML?

Remembering that all classes are instances of the Class class, is this
possible? I want to create some class named, say C1, which is empty (or
perhaps with one or two class instance vars) by loading a YAML document.
I can’t seem to create instances of Class class in YAML, while I can
easily do this in irb:

$ irb
irb(main):001:0> C1=Class.new
=> C1
irb(main):002:0> C1.new
=> #C1:0x40174a1c

···


dave

irb(main):002:0> 5.to_yaml
=> “— 5”
irb(main):003:0> Class.to_yaml
ArgumentError: can’t dump anonymous class Class
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:8:in to_yaml' from (irb):3 irb(main):004:0> class Foo; end => nil irb(main):005:0> Foo.to_yaml ArgumentError: can't dump anonymous class Class from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:8:in to_yaml’
from (irb):5

Does not appear possible to do with Yaml, but you can with Marshal:

irb(main):007:0> Marshal.dump(Foo)
=> “\004\010c\010Foo”

···

David Garamond (lists@zara.6.isreserved.com) wrote:

Remembering that all classes are instances of the Class class, is this
possible? I want to create some class named, say C1, which is empty (or
perhaps with one or two class instance vars) by loading a YAML document.
I can’t seem to create instances of Class class in YAML, while I can
easily do this in irb:


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

C1 = YAML.load(“— !ruby/object:Class {}”)

The easiest way to figure out how to represent something in Ruby in
YAML is just to call #to_yaml on your object:

require ‘yaml’
C1 = Class.new
puts C1.to_yaml #=> — !ruby/object:Class {}

-rich

···

On Tuesday, September 23, 2003, at 03:01 PM, David Garamond wrote:

Remembering that all classes are instances of the Class class, is this
possible? I want to create some class named, say C1, which is empty
(or perhaps with one or two class instance vars) by loading a YAML
document. I can’t seem to create instances of Class class in YAML,
while I can easily do this in irb:

$ irb
irb(main):001:0> C1=Class.new
=> C1
irb(main):002:0> C1.new
=> #C1:0x40174a1c


dave

My bad…I was using an older version of YAML…in 1.8 this does not
work :frowning:

-rich

···

On Tuesday, September 23, 2003, at 03:13 PM, Richard Kilmer wrote:

— !ruby/object:Class {}