Created
June 24, 2011 05:43
-
-
Save EmmanuelOga/1044286 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 'gsl' | |
require 'yaml' | |
v = GSL::Vector.alloc 10 | |
0.upto(9) do |n| v[n] = n* 10 end | |
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.seq( taguri, to_yaml_style ) do |map| | |
to_a.each_with_index { |v, i| map.add(v) } | |
end | |
end | |
end | |
end | |
YAML::add_domain_type( "ruby.com,2011", "gsl-vector") do |type, val| | |
vec = GSL::Vector.alloc val.length | |
val.each_with_index { |v, i| vec[i] = v } | |
vec | |
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