Newbie: yaml array of arrays

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

···

On 12/09/06, allan.m.miller@gmail.com <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.

---
-
  - a
  - b
  - c
-
  - d
  - e
  - f
-
  - g
  - h
  - i
=>nil

Farrel

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

···

On 06-09-12, at 03:40, 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):
<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?

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

Then try:

YAML.load_file 'your_file.yaml'

--
Jeremy Tregunna
jtregunna@blurgle.ca

allan.m.miller@gmail.com 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"]]

--
Bruno Michel

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

···

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.

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

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.

Or a two-dimensional array.

I've tried to create this yaml file in a text editor (test.cfg):

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

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

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?

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:

···

allan.m.miller@gmail.com wrote:

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

--
Paul Lutus
http://www.arachnoid.com

Paul Lutus 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 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.

DarnitnowIunderst
andwhyIhavesom
uchtroublewithEn
glishpunctation

<G>

···

On 9/12/06, Paul Lutus <nospam@nosite.zzz> wrote:

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

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Ola Bini wrote:

Paul Lutus 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.

···

--
Paul Lutus
http://www.arachnoid.com

Rick DeNatale wrote:

···

On 9/12/06, Paul Lutus <nospam@nosite.zzz> wrote:

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

DarnitnowIunderst
andwhyIhavesom
uchtroublewithEn
glishpunctation

Point taken. :slight_smile:

-----------------------------------------------

#!/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.)

--
Paul Lutus
http://www.arachnoid.com