Skip to content

Instantly share code, notes, and snippets.

@agrimm
Created March 18, 2011 13:20
Show Gist options
  • Save agrimm/876052 to your computer and use it in GitHub Desktop.
Save agrimm/876052 to your computer and use it in GitHub Desktop.
# Code is from http://opensoul.org/2010/5/3/the-quest-for-the-holy-object-serialization-format
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
# Under YAML
require "yaml"
yaml_created_file_class = YAML.load(YAML.dump(File)) # => File
yaml_created_file_class.methods.grep(/exist/) # => ["exist?", "exists?"]
# Under ZAML
require "zaml"
zaml_created_file_class = YAML.load(ZAML.dump(File)) # => #<Class:0x7f950e8fabd8>
zaml_created_file_class.methods.grep(/exist/) # => []
RUBY_VERSION # => "1.8.7"
defined?(RUBY_ENGINE) # => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment