Skip to content

Instantly share code, notes, and snippets.

@bradland
Created February 12, 2013 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradland/4772990 to your computer and use it in GitHub Desktop.
Save bradland/4772990 to your computer and use it in GitHub Desktop.
require 'yaml'
case RUBY_VERSION[0,3]
when "1.9"
YAML::ENGINE.yamler = 'syck'
yaml_engine = YAML::ENGINE.yamler
when "1.8"
yaml_engine = 'syck'
end
puts %[
Let's see if we can serialize arbitrary objects with YAML
We're using:
Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}
#{yaml_engine} YAML engine
]
# Create some arbitrary object that we'll try to serialize
class Blox
attr_accessor :colors, :height, :width, :meta
end
blox = Blox.new
blox.colors = %w[red green blue]
blox.height = "10"
blox.width = "10"
blox.meta = {:owner => "John Doe", :return_to => "Central storage"}
puts "=== Begin YAML.dump ===\n\n"
puts YAML.dump(blox)
puts %[
===End YAML.dump===
If you saw the object output above, your YAML engine is capable of serializing arbitrary objects.
]
@bradland
Copy link
Author

Let's see if we can serialize arbitrary objects with YAML

We're using:
  Ruby 1.8.7-p352
  syck YAML engine

=== Begin YAML.dump ===

--- !ruby/object:Blox 
colors: 
- red
- green
- blue
height: "10"
meta: 
  :owner: John Doe
  :return_to: Central storage
width: "10"

===End YAML.dump===

If you saw the object output above, your YAML engine is capable of serializing arbitrary objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment