-
-
Save kitop/1045404 to your computer and use it in GitHub Desktop.
GSL::Vector to YAML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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