Creating classes with from YAML?

D

David Garamond

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>
 
E

Eric Hodel

--3V7upXqbjpZ4EhLz
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Remembering that all classes are instances of the Class class, is this=20
possible? I want to create some class named, say C1, which is empty (or= =20
perhaps with one or two class instance vars) by loading a YAML document.= =20
I can't seem to create instances of Class class in YAML, while I can=20
easily do this in irb:

irb(main):002:0> 5.to_yaml
=3D> "--- 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
=3D> 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)
=3D> "\004\010c\010Foo"


--=20
Eric Hodel - (e-mail address removed) - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


--3V7upXqbjpZ4EhLz
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (FreeBSD)

iD8DBQE/cJrDMypVHHlsnwQRAkvJAJ9N6nrkRwQF1ezrs7ZDliS7YBHt9gCgzVLx
DWyJ7VxG/eqRXHg/yvhmMhM=
=swIg
-----END PGP SIGNATURE-----

--3V7upXqbjpZ4EhLz--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top