From: Ola Bini Date: 2006-09-12T16:54:18+09:00 Subject: Re: newbie: yaml array of arrays allan.m.miller@gmail.com wrote: > Hello, > > I'm new to yaml, and yaml in Ruby. > > I'm trying to create a yaml file in a text editor, one part of which > will consist of an array of of arrays. The data structure I'm trying > to represent is: > > [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']] > > an array of three arrays. > > I've tried to create this yaml file in a text editor (test.cfg): > > -- a > - b > - c > -- d > - e > - f > -- g > - h > - i > > and read it in as follows > > require 'yaml' > na = open('test.cfg') { |f| YAML.load(f) } > > I get a load error: > > $ ./test.rb > /usr/lib/ruby/1.8/yaml.rb:119:in `load': parse error on line 9, col -1: > `' (ArgumentError) > from /usr/lib/ruby/1.8/yaml.rb:119:in `load' > from ./test.rb:3 > from ./test.rb:3:in `open' > from ./test.rb:3 > > The last line seems o be causing an ArgumentError. > > I'm sure my approach is completely off - how does one represent an > array of arrays in yaml, and load it into a Ruby array of arrays? > > Thanks. > > > Hi, This approach should work without trouble. Do this: puts [%w(a b c), %w(d e f), %w(g h i)].to_yaml your output should be --- - - a - b - c - - d - e - f - - g - h - i Another way is to use the flow-style like this: --- - [a, b, c] - {d, e, f] - [g, h, i] -- Ola Bini (http://ola-bini.blogspot.com) JvYAML, RbYAML, JRuby and Jatha contributor System Developer, Karolinska Institutet (http://www.ki.se) OLogix Consulting (http://www.ologix.com) "Yields falsehood when quined" yields falsehood when quined.