From: Daniel Martin Date: 2007-08-19T22:45:57+09:00 Subject: Re: YAML and NArray Daniel Martin writes: > So anyway, I finally integrated YAML and NArray, and present the > solution here for other people googling in the future on how to get > these two to work together, as well as now for comment and criticism > by current list subscribers. Note that it handles NArray, NMatrix, > and NVector objects: And this is what I get for posting something other than the exact code I tested with. Apparently there's a bug in Syck such that email-based tags are treated differently from dns-based tags. Unfortunately, I don't own a domain name - fortunately, I know the guy who does own snowplow.org, and I'm pretty sure he'd be okay with me using the tag prefix shown below. So here's a fixed version. Note that it is the same as before, except that the private tag used in the yaml_as line is different. ########## narray-yaml.rb ######### require 'yaml' require 'narray' # First, a yaml utility routine to make the output prettier module YAML class YIArray < Array yaml_as "tag:yaml.org,2002:seq" def to_yaml_style; :inline; end end def YAML.inline_array(arr) if Array === arr[0] then arr.map {|s| inline_array(s)} else YIArray.new.concat(arr) end end end class NArray # I used a tag of my own since I wasn't sure if # someone else had their own serialization of # NArray objects yaml_as "tag:snowplow.org,2007:martin/narray" def to_yaml( opts = {} ) YAML::quick_emit( self.object_id, opts ) do |out| out.map(taguri) do |map| map.add('typecode', typecode) map.add('shape', YAML::inline_array(shape)) map.add('data', YAML::inline_array(to_a)) end end end def self.yaml_new(klass, tag, val) result = klass.new(val['typecode'], *(val['shape'])) result[] = val['data'] result end end __END__ Hopefully this message will be found by people googling for how to integrate YAML and NArray. I'm going to make this file available at http://snowplow.org/martin/narray/ along with standard verbiage up at the top placing it under some variant of the MIT license or the no-attribution BSD license. -- s=%q( Daniel Martin -- martin@snowplow.org puts "s=%q(#{s})",s.to_a.last ) puts "s=%q(#{s})",s.to_a.last