# Newbie: yaml array of arrays

**URL:** <https://www.ruby-forum.com/t/newbie-yaml-array-of-arrays/68386>\
**Category:** Ruby\
**Created:** [September 12, 2006, 9:40am UTC](https://www.ruby-forum.com/t/newbie-yaml-array-of-arrays/68386 "2006-09-12T09:40:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 12, 2006, 9:40am UTC](https://www.ruby-forum.com/t/newbie-yaml-array-of-arrays/68386/1 "2006-09-12T09:40:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 12, 2006, 9:48am UTC](https://www.ruby-forum.com/t/newbie-yaml-array-of-arrays/68386/2 "2006-09-12T09:48:19Z")

</div>

On 12/09/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> an array of three arrays.
> 
> - h  
> /usr/lib/ruby/1.8/yaml.rb:119:in `load’: parse error on line 9, col -1:
> 
> 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 12, 2006, 9:48am UTC](https://www.ruby-forum.com/t/newbie-yaml-array-of-arrays/68386/3 "2006-09-12T09:48:19Z")

</div>

On 06-09-12, at 03:40, removed\_email\_address@domain.invalid wrote:

> an array of three arrays.
> 
> I’ve tried to create this yaml file in a text editor (test.cfg):  
>   
> 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’

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 12, 2006, 9:48am UTC](https://www.ruby-forum.com/t/newbie-yaml-array-of-arrays/68386/4 "2006-09-12T09:48:19Z")

</div>

removed\_email\_address@domain.invalid a écrit :

> an array of three arrays.
> 
> - h  
> /usr/lib/ruby/1.8/yaml.rb:119:in `load’: parse error on line 9, col -1:
> 
> Thanks.

Hello,

> > YAML.load \<\<-EOS

- 
  - a
  - b
  - c

- 
  - d
  - e
  - f

- 
  - g
  - h
  - i  
EOS  
=\> [[“a”, “b”, “c”], [“d”, “e”, “f”], [“g”, “h”, “i”]]

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 12, 2006, 9:54am UTC](https://www.ruby-forum.com/t/newbie-yaml-array-of-arrays/68386/5 "2006-09-12T09:54:19Z")

</div>

removed\_email\_address@domain.invalid wrote:

> an array of three arrays.
> 
> - h  
> /usr/lib/ruby/1.8/yaml.rb:119:in `load’: parse error on line 9, col -1:
> 
> 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 B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
JvYAML, RbYAML, JRuby and Jatha contributor  
System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))

“Yields falsehood when quined” yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 12, 2006, 9:56am UTC](https://www.ruby-forum.com/t/newbie-yaml-array-of-arrays/68386/6 "2006-09-12T09:56:20Z")

</div>

removed\_email\_address@domain.invalid wrote:

> an array of three arrays.  
> Or a two-dimensional array.

> - i  
> Can I ask what guidance you have that this is valid YAML syntax?

> ```
> 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?

We are faced here with a question of how one converts a hand-entered,  
plain-text file that we hope is valid YAML, into a Ruby array. How shall  
we  
solve this problem?

Well, we could type a bunch of stuff into a text editor, run it through  
a  
YAML interpreter, and see if it turns into the desired array when  
parsed.  
This could take a long time.

But how about this? Why don’t we attack this problem from the other  
direction? Why don’t we take the original array, convert it into YAML,  
and  
see how it looks?

#!/usr/bin/ruby

require ‘yaml’

array = [[‘a’, ‘b’, ‘c’], [‘d’, ‘e’, ‘f’], [‘g’, ‘h’, ‘i’]]

YAML.dump(array,STDOUT)

Output:

* * *

- 
  - a
  - b
  - c

- 
  - d
  - e
  - f

- 
  - g
  - h
  - i

Something tells me (even thought I haven’t tried it) that, if I take  
this  
YAML output and use it as an input, the original array will be  
recreated.

One more question. Why are you trying to create YAML data by hand? True,  
YAML data is human-readable, but that doesn’t guarantee that it’s  
human-writable.

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 12, 2006, 10:10am UTC](https://www.ruby-forum.com/t/newbie-yaml-array-of-arrays/68386/7 "2006-09-12T10:10:22Z")

</div>

Paul L. wrote:

> Parting shot. Confucius say, “avoid data formats in which whitespace is  
> syntactically significant.”

Whitespace in YAML is only significant when using block style data.

–  
Ola B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
JvYAML, RbYAML, JRuby and Jatha contributor  
System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))

“Yields falsehood when quined” yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 12, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/newbie-yaml-array-of-arrays/68386/8 "2006-09-12T19:01:30Z")

</div>

On 9/12/06, Paul L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Parting shot. Confucius say, “avoid data formats in which whitespace is  
> syntactically significant.”

DarnitnowIunderst  
andwhyIhavesom  
uchtroublewithEn  
glishpunctation

-- Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 12, 2006, 10:40am UTC](https://www.ruby-forum.com/t/newbie-yaml-array-of-arrays/68386/9 "2006-09-12T10:40:25Z")

</div>

Ola B. wrote:

> Paul L. wrote:
> 
> > Parting shot. Confucius say, “avoid data formats in which whitespace is  
> > syntactically significant.”
> 
> Whitespace in YAML is only significant when using block style data.

Well, it was significant in this case – it prevented correct  
interpretation  
of your entries. Those missing spaces killed the result.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 12, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/newbie-yaml-array-of-arrays/68386/10 "2006-09-12T19:51:36Z")

</div>

Rick DeNatale wrote:

> On 9/12/06, Paul L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Parting shot. Confucius say, “avoid data formats in which whitespace is  
> > syntactically significant.”
> 
> DarnitnowIunderst  
> andwhyIhavesom  
> uchtroublewithEn  
> glishpunctation

Point taken. 🙂

* * *

#!/usr/bin/ruby

data=\<\<EOF  
I’ve always thought languages and data formats that rely on whitespace  
represent going one meter too far into the realm of making the data look  
user-friendly.

EOF

puts data

data.gsub!(/\s+/,“”).gsub!(/(.{1,5})/,"\1 ")

puts data

* * *

Output:

I’ve always thought languages and data formats that rely on whitespace  
represent going one meter too far into the realm of making the data look  
user-friendly.

I’vea lways thoug htlan guage sandd atafo rmats thatr elyon white space  
repre sentg oingo nemet ertoo farin tothe realm ofmak ingth edata looku  
ser-f riend ly.

(just to undermine my own point.)
