Skip to content

Instantly share code, notes, and snippets.

@kitop
Forked from EmmanuelOga/gslyaml.rb
Created June 24, 2011 18:47
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 kitop/1045404 to your computer and use it in GitHub Desktop.
Save kitop/1045404 to your computer and use it in GitHub Desktop.
GSL::Vector to YAML
# http://tech.natemurray.com/2007/03/custom-yaml-emitter.html
require 'psych'
require 'yaml'
require 'gsl'
v = GSL::Vector.alloc 1..10
class GSL::Vector
def to_yaml_type
"!ruby.com,2011/gsl-vector"
end
def to_yaml( opts = {} )
YAML.quick_emit( self.object_id, opts ) do |out|
out.map( to_yaml_type ) do |map|
map.add("values", self.to_a)
end
end
end
end
YAML::add_domain_type( "ruby.com,2011", "gsl-vector") do |type, val|
GSL::Vector.alloc val["values"]
end
puts v == YAML.load(YAML.dump(v))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment