newbie: yaml array of arrays

A

allan.m.miller

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.
 
F

Farrel Lifson

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.

You can use IRB to work it out in reverse... start with the ruby array
and generate teh yaml from it.

irb(main):004:0>require 'yaml'
=>true
irb(main):005:0> puts [['a','b','c'],['d','e','f'],['g','h','i']].to_yaml
---
-
- a
- b
- c
-
- d
- e
- f
-
- g
- h
- i
=>nil

Farrel
 
J

Jeremy Tregunna

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):
<snip>
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?

You wanted YAML.load_file not YAML.load; and a yaml file that looks
like this:

---
- - a
- b
- c
- - d
- e
- f
- - g
- h
- i

Then try:

YAML.load_file 'your_file.yaml'
 
B

Bruno Michel

(e-mail address removed) a écrit :
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.
Hello,
YAML.load <<-EOS
-
- a
- b
- c
-
- d
- e
- f
-
- g
- h
- i
EOS
=> [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
 
O

Ola Bini

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.
 
R

Rick DeNatale

Parting shot. Confucius say, "avoid data formats in which whitespace is
syntactically significant."

DarnitnowIunderst
andwhyIhavesom
uchtroublewithEn
glishpunctation

<G>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top